Skip to content
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

Declare a type with a JSON file instead of an interface #23898

Closed
ORESoftware opened this issue May 4, 2018 · 8 comments
Closed

Declare a type with a JSON file instead of an interface #23898

ORESoftware opened this issue May 4, 2018 · 8 comments
Labels
Duplicate An existing issue was already created

Comments

@ORESoftware
Copy link

Search Terms

typescript json interface file

Suggestion

I have this question on SO:
https://stackoverflow.com/questions/50181254/static-typing-with-json-and-typescript?noredirect=1#comment87380729_50181254

Why can't we declare a TS type based on a .json file? Something like this:

export type MyInterface = typeof 'path/to/json/file.json'

it seems like a no-brainer to me, but maybe I am stupid.

Use Cases

Keep things DRY. Use JSON files as interfaces.

Examples

Done did.

Checklist

My suggestion meets these guidelines:
[x] This wouldn't be a breaking change in existing TypeScript / JavaScript code
[x] This wouldn't change the runtime behavior of existing JavaScript code
[x] This could be implemented without emitting different JS based on the types of the expressions
[x] This isn't a runtime feature (e.g. new expression-level syntax)

@mhegazy
Copy link
Contributor

mhegazy commented May 4, 2018

You are in luck! #22167 just got merged, and with --resolveJsonModule you can load the json object in your program and use its type..

with import types too you can capture that type in a type alias:

type schemaType = typeof import("./schema.json");

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 4, 2018
@ORESoftware
Copy link
Author

nice that is cool, how do we know when the next tsc version will be published on NPM? is there some notification system?

@mhegazy
Copy link
Contributor

mhegazy commented May 5, 2018

It is published nightly around midnight PST

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@ORESoftware
Copy link
Author

@mhegazy one potential improvement!

tsc can now use .json files for types - but can tsc use json-schema files to determine generic types?

for example, say I have this JSON file:

{
  "foo":"bar"
}

now, foo the interface for the above could be:

interface JSON {
  foo: "bar"
}

or it could be:

interface JSON {
  foo: string
}

which does tsc currently do? Could tsc be updated to handle json-schema from
http://json-schema.org/

?

@ORESoftware
Copy link
Author

@mhegazy what about importing json files and getting type info? If I do:

import json = require('../../my.json')

and also use:

"resolveJsonModule": true,

in my tsconfig.json. My project no longer compiles correctly. Is this a known problem? Should I file a new ticket?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 25, 2018

Should I file a new ticket?

yes please.

@ORESoftware
Copy link
Author

@mhegazy thanks, I filed the issue here: #25216

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants