镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

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)

Activity

  1. mhegazy commented on May 4, 2018

    @mhegazy
    Contributor

    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");
  2. ORESoftware commented on May 5, 2018

    @ORESoftware
    Author

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

  3. mhegazy commented on May 5, 2018

    @mhegazy
    Contributor

    It is published nightly around midnight PST

  4. typescript-bot commented on May 21, 2018

    @typescript-bot
    Contributor

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

  5. ORESoftware commented on Jun 16, 2018

    @ORESoftware
    Author

    Mohamed Hegazy (@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/

    ?

  6. ORESoftware commented on Jun 25, 2018

    @ORESoftware
    Author

    Mohamed Hegazy (@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?

  7. mhegazy commented on Jun 25, 2018

    @mhegazy
    Contributor

    Should I file a new ticket?

    yes please.

  8. ORESoftware commented on Jun 26, 2018

    @ORESoftware
    Author

    Mohamed Hegazy (@mhegazy) thanks, I filed the issue here: #25216

  9. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions