Skip to content

inaccurate string type from json file while resolveJsonModule option on聽#27913

Description

@baivoom

TypeScript Version: 3.1.1
VS Code Version: 1.28.1

Search Terms: resolveJsonModule

Code
./schema.json

{
    "type": "object"
}

./test.ts

import schema = require('./schema.json')
// give a restriction only the string value 'object' is acceptable
type Format = { type: 'object' } 
// here the vscode gives the following error
// ; Type { type: string } is not assignable to Format
// ;     Type 'string' is not assignable to  type ' "object" '
const data: Format  =  schema 

Expected behavior:
The imported string type from json file should be converted as:

From: { type: "object" }
To: type FromJson = { type: string & 'object' }

As a result, the code below should not be given warnings or errors:

// imported from json
type FromJson = { type: string & 'object' }
const json: FromJson = { type: 'object' }

// type restriction
type FromTs = { type: 'object' }

//  no error here
const ts: FromTs = json

// no side effect
type StringType = { type: string }
const acceptable: StringType = json

Actual behavior:
error on vscode, but pass the compilation

Others:
1
The typescript 3.03 does not give this error, but it is still not correct because it does not distinguish
{ type: 'object1' } from json and type { type: 'object'} from ts code.
2
just considering how to give the type from json file if the string value is very long
the type { type: string & 'this is a very very very very very very very very long'} seems ugly

Related Issues: Not found

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions