You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importschema= require('./schema.json')// give a restriction only the string value 'object' is acceptabletypeFormat={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" 'constdata: 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 jsontypeFromJson={type: string&'object'}constjson: FromJson={type: 'object'}// type restrictiontypeFromTs={type: 'object'}// no error hereconstts: FromTs=json// no side effecttypeStringType={type: string}constacceptable: 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
The text was updated successfully, but these errors were encountered:
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" }.
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?
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 Version: 3.1.1
VS Code Version: 1.28.1
Search Terms: resolveJsonModule
Code
./schema.json
./test.ts
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:
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 uglyRelated Issues: Not found
The text was updated successfully, but these errors were encountered: