Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add option to always inline .json type import #61614

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

Open
6 tasks done
maxpatiiuk opened this issue Apr 25, 2025 · 1 comment
Open
6 tasks done

Add option to always inline .json type import #61614

maxpatiiuk opened this issue Apr 25, 2025 · 1 comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@maxpatiiuk
Copy link

maxpatiiuk commented Apr 25, 2025

πŸ” Search Terms

json type import inline library resolveJsonModule

βœ… Viability Checklist

⭐ 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

  1. What do you want to use this for?
    • I want to use .json type imports for getting types of translation files and configuration files
  2. 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
  3. 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:

@MartinJohns
Copy link
Contributor

Technically it goes against goal 7:

Preserve runtime behavior of all JavaScript code.

And one could argue non-goal 4:

Provide an end-to-end build pipeline. Instead, make the system extensible so that external tools can use the compiler for more complex build workflows.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants