Open
Description
π Search Terms
json type import inline library resolveJsonModule
β Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Add a tsconfig.json option to force .json type imports to inline the inferred .json type, rather than preserve .json import inside emitted .d.ts
or always inline without introducing a new option
or enable #resolveJsonModule by default
π Motivating Example
JSON type imports are a very convenient feature for getting a type of localization strings to get intellisense when dealing with translation strings:
// The .json for correct locale will be loaded at runtime - but we need a type at compile time
import type T9nStrings from "../assets/t9n/messages.en.json";
However, as a library author, that presents two issues because TypeScript may preserve the above type import in the .d.ts file:
- Unless every consumer has #resolveJsonModule enabled, their typescript will error out when trying to resolve the .json type import from my .d.ts
- I need to make sure the above .json file is also accessible relative to the final .d.ts file I distribute in dist/
π» Use Cases
- What do you want to use this for?
- I want to use .json type imports for getting types of translation files and configuration files
- What shortcomings exist with current approaches?
- the .json type import is inlined some of the time only. when not inlined, it can break my consumers
- What workarounds are you using in the meantime?
- the following hack seems to trigger typescript to always inline the .json file rather than preserve the type import:
import type T9nStrings from "./assets/t9n/messages.en.json";
const identity = <T>(value: T): T => value;
const messages = identity<typeof T9nStrings>;
type Messages = ReturnType<typeof messages>;
related issues: