Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
baivoom opened this issue Oct 15, 2018 · 4 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@baivoom
Copy link

baivoom commented Oct 15, 2018

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

@sheetalkamat
Copy link
Member

Module type is always widened on import to that we aren't exposing private information in the json: eg. { name: "Some name", age: 55 } should be typed as { name: string, age: number }. This behavior is also same if for eg. you had js file instead of json with module.exports = { type: "object" }.

@sheetalkamat sheetalkamat added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 15, 2018
@baivoom
Copy link
Author

baivoom commented Oct 15, 2018

I did not notice the design of "do not exposing private information". In this case, according to this design, the 'data-reflected' type exported whatever from the json or ts file will face the same problem, we can not give particular string values as type, for example, type Type = 'object'|'array'|'string', to restrict the input data . Does anyone have the alternative way to workaround?

@andykais
Copy link

came across this myself. I have many large json config files checked into a particular project, whos content is not sensitive. It is currently not possible to check those file's fields for string literals unless I check the files at runtime with ts-runtime. I want to be able to check the types of said files at compile time.

Perhaps a compiler flag like widenModuleTypes defaulted to true would be possible? If this is an intentional behavior it sounds like it is possible to change, just not desired as default behavior.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants