Suggestion
π Search Terms
json schema
β
Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Currently when importing JSON the types provided is basically just the inferred type treated the JSON as an object literal.
It would however be helpful to support JSON schema declared types to create types that may be either be more narrow or less narrow that more accurately reflect what one can expect with the JSON.
π Motivating Example
Consider as one example the following JSON:
When we import it, we are unable to use this as a general record object despite that being the intention of the value:
import mimeMap from "./mime-map.json" assert { "type": "json" };
const extension: string = getExtensionSomehow();
// Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ js: string; mjs: string; cjs: string; json: string; }'.
const mimeType = mimeMap.mimeTypes[extension];
if (mimeType === undefined) {
// ...Unrecognized file type
} else {
// ...do something with the type
}
This happens because the inferred type is too narrow, now there's no way that TS could know this directly, however JSON schema is an existing mechanism for declaring such types.
For example if I were to define the schema:
This would effectively declare a JSON object of type:
{
mimeTypes: Record<string, string>
}
Existing tooling already has support for such schemas, for example VSCode can already recognize incorrect types in a file which such a schema:

It would be nice if TS could also recognize JSON schema types, and generate appropriate TS types for them so that types when imported correspond more appropriately.
π» Use Cases
The core use case is that JSON types would more accurately represented by TS recognizing a JSON Schemas, this would allow the JSON file to evolve more so with TS still being able to receive correct type information about the contents rather than just a best guess based on the object shapes.
Notes
This is a potentially breaking change for existing JSON imports, however in most cases people would probably want the JSON Schema they are using. If there was significant breakage an opt-in might be neccessary e.g. useJSONSchema in tsconfig or similar.
Suggestion
π Search Terms
json schema
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Currently when importing JSON the types provided is basically just the inferred type treated the JSON as an object literal.
It would however be helpful to support JSON schema declared types to create types that may be either be more narrow or less narrow that more accurately reflect what one can expect with the JSON.
π Motivating Example
Consider as one example the following JSON:
When we import it, we are unable to use this as a general record object despite that being the intention of the value:
This happens because the inferred type is too narrow, now there's no way that TS could know this directly, however JSON schema is an existing mechanism for declaring such types.
For example if I were to define the schema:
This would effectively declare a JSON object of type:
Existing tooling already has support for such schemas, for example VSCode can already recognize incorrect types in a file which such a schema:
It would be nice if TS could also recognize JSON schema types, and generate appropriate TS types for them so that types when imported correspond more appropriately.
π» Use Cases
The core use case is that JSON types would more accurately represented by TS recognizing a JSON Schemas, this would allow the JSON file to evolve more so with TS still being able to receive correct type information about the contents rather than just a best guess based on the object shapes.
Notes
This is a potentially breaking change for existing JSON imports, however in most cases people would probably want the JSON Schema they are using. If there was significant breakage an opt-in might be neccessary e.g.
useJSONSchemaintsconfigor similar.