microsoft / microsoft/TypeScript
Add option to always inline .json type import
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔍 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 tsconfig.json 中关于 resolveJsonModule 的文档以及相关 issue #60647 和 #43940。追踪 JSON 类型导入在生成的 .d.ts 文件中是如何表示的,然后确定所提议的选项或默认行为应如何影响内联。选择此行为后,如果库声明不再要求使用者解析原始 .json 文件,则视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100