-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Consider annotations and compilation of .json files #2064
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
Comments
This is an idea we've discussed before and I think it has a lot of merit. I would propose that the type name be specified as part of the /// <reference path="user.d.ts" type="user.User"/>
{
"name": "Joe Bloggs",
...
} A number of JSON processing tools already tolerate comments (but definitely wouldn't tolerate actual identifiers before the object literal). |
I should clarify... The thing I find useful here is the type checking and associated IDE services such as statement completion. I'm less sure about producing "corrected" output. |
@ahejlsberg, no I meant "after the user has corrected the errors", not for the compiler to correct it. If I understand you correctly, with the This is cleaner than having to generate emit, but would require stripping out the var json = require('user.json'); // SyntaxError: user.json: Unexpected token / I like the idea of adding the type annotation in the |
Currently I use type assertion syntax to enforce type check on a object literal:
Is this what you want? |
@duanyao, that is not valid syntax in a |
After compilation, it is a valid json. You presented an example of |
Are you suggesting that as an alternative syntax? It's not clear from the statement "Is that what you want?" The problem with the type assertion syntax is that here we are attempting to annotate JSON - not coerce it into something. |
No. I suggest that you write |
In that case there is nothing new being suggested. |
Why need something new if existing tools can solve the problem? Or are there problems I overlooked? |
A JSON file is not equivalent to a JavaScript file. There is no top-level variable. That's probably what you're missing. |
Top-level variable is not the issue. I just tested tsc: <user.User>{
"name": "Joe Bloggs",
"address": {
"house": 1,
"street": "London",
},
"age": 55
} was compiled into {
"name": "Joe Bloggs",
"address": {
"house": 1,
"street": "London"
},
"age": 55
}; You see, the only problem is the trailing |
That's interesting. But still won't do. JSON is a subset of JavaScript and things that are permissible in JavaScript is not valid JSON.
|
These format issues should also be addressed by "json mode" of tsc. Data type issues like |
Like I said there is nothing new being suggested. |
@mhegazy, @RyanCavanaugh any chance of adding this to the discussion list now that |
Hi, guys, is this on the radar? |
We have not discussed this in a while. I believe we still need a detailed proposal of how this can be accomplished. One other issue that seems more appealing is #7071, this requires no annotation, it just allows the compiler to load these files and infer their types. you would get an error if you were to assign the contents of the file to a variable of an unmatching type. obviously this only works for node, so that might not be what you are looking for. |
@mhegazy, #7071 is a special case that unfortunately doesn't make things any better from our perspective. The use-case I outlined is to do with loading JSON resources via HTTP and using them on a client application. We just want to ensure the types used in the client correspond to the JSON file stored on the server. I'd say this is a common requirement. The only way I can think of to accomplish this at the moment is to have a new file type |
I think the use case here is well-handled by a few things now:
Given lack of other feedback here, I don't see us proceeding anytime soon. |
Note that JSON type inference doesn't always work as expected, and can mis-match types, even when the data in a JSON exactly matches an interface. See: #54488 |
Motivation
The shape of JSON stored in files can be validated using JSON schema. However, JSON schema cannot be used to ensure the stored JSON corresponds to a specific TypeScript interface.
Understandably, most JSON tends to be stored in databases. However, there are many cases where small documents are stored on the file system in order to permit quick editing. Two common examples are config and resource files. The JSON in these files is loaded at runtime and accessed by JavaScript code, thereby necessitating they correspond to specific TypeScript interfaces.
We have a small but significant use case for this scenario.
Proposal
Interface declarations file
user.d.ts
TypeScript JSON file
user.json.ts
Compiled output (after errors are corrected): File
user.json
The text was updated successfully, but these errors were encountered: