microsoft / microsoft/TypeScript
import ConstJson from './config.json' as const;
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Suggestion
The ability to get const types from a JSON configuration file.
If the json is:
{
"appLocales": ["FR","BE"]
}
I want to import the JSON and get a {appLocales: "FR" | "BE"} type instead of string
As suggested here, this could rely on ES import attributes (stage 4, ES2025, TypeScript 5.3):
import data from "./data.json" with { type: "json", const: true };
Use Cases
The current approach gives a too broad type string. I understand it makes by default, but having the possibility to import a narrower type would be helpful: it would permit me to avoid maintaining both a runtime locale list + a union type that contains the values that are already in the list, ensuring my type and my runtime values are in sync.
It is possible to type a JSON file explicitly using allowArbitraryExtensions: true and a .d.json.ts file, but this doesn't permit to infer the type from the JSON directly, requires explicit typing and maintenance.
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Links:
This feature has been mentioned:
- by @qm3ster and @maartenth https://github.com/microsoft/TypeScript/pull/29510#issuecomment-456833107
- by @m-b-davis https://github.com/Microsoft/TypeScript/issues/26552#issuecomment-496925984
Search Terms
json const assertion import attributes with allowArbitraryExtensions
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず issue の議論、TypeScript 5.3 の import-attributes に関するドキュメント、および既存の allowArbitraryExtensions アプローチを確認してください。提案された const 属性を使用する JSON import で、既存の import や出力される JavaScript を変更せずに、"FR" | "BE" のようなリテラル値を推論できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100