microsoft / microsoft/TypeScript

Add option to always inline .json type import

Ouverte
#61,614 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

### 🔍 Search Terms

json type import inline library resolveJsonModule

### ✅ Viability Checklist

- [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. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] 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](https://www.typescriptlang.org/tsconfig/#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:

```ts
// 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](https://www.typescriptlang.org/tsconfig/#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:

```ts
import type T9nStrings from "./assets/t9n/messages.en.json";
const identity = (value: T): T => value;
const messages = identity;
type Messages = ReturnType;
```

related issues:
- https://github.com/microsoft/TypeScript/issues/60647
- https://github.com/microsoft/TypeScript/issues/43940

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par lire la documentation de tsconfig.json concernant resolveJsonModule ainsi que les issues associées #60647 et #43940. Suivez la manière dont l’importation de type JSON est représentée dans les fichiers .d.ts générés, puis déterminez comment l’option proposée ou le comportement par défaut devrait affecter l’inlining. Le travail est terminé lorsque les déclarations de bibliothèque n’exigent plus des consommateurs qu’ils résolvent le fichier .json d’origine lorsque ce comportement est sélectionné.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.