Typescript: allow importing json with resolveJsonModule "as const"

Created on 12 Sep 2019  ·  1Comment  ·  Source: microsoft/TypeScript

Search Terms

resolveJsonModule, const

Suggestion

Allow setting "resolveJsonModule": "const" as well as false and true.

as const is very useful, as is resolveJsonModule. I should be able to use them together.

Use Cases

I have some type helpers that can build strongly-typed dictionaries from some seed data whose schema is in another domain. We'd like to be able to use the json for things outside typescript, so would rather not put it in a .ts file. resolveJsonModule is almost what we need, but we can't import as const.

Examples

Assuming that seed data is defined like this:

const data = {
  items: [{ id: 'alice', type: 'person' }, { id: 'bob', type: 'person' }, { id: 'fido', type: 'pet' }]
} as const

const testHelper = // ...lots of type magic, taking advantage of the types in `items` being {id: "alice", type: "person"} etc., not just {id: string; type: string}

testHelper.person.alice // {id: 'alice', type: 'person'}

Checklist

My suggestion meets these guidelines:

  • [ ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [ ] This wouldn't change the runtime behavior of existing JavaScript code
  • [ ] This could be implemented without emitting different JS based on the types of the expressions
  • [ ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [ ] This feature would agree with the rest of TypeScript's Design Goals.

Most helpful comment

>All comments

Was this page helpful?
0 / 5 - 0 ratings