microsoft / microsoft/TypeScript
Feature: Const-literal typing for import-attribute imports
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
🔍 Search Terms
Keywords: Import Attributes, Const Literal Typing, Type Providers, DSL Integration, JSON Schema
Related Issues
- https://github.com/microsoft/TypeScript/issues/3136
- https://github.com/microsoft/TypeScript/issues/32063
- https://github.com/microsoft/TypeScript/issues/26552
✅ 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
It should be possible to import a fully typed value representation via import-attributes
Problem
When importing Json with import-attributes, TypeScript produces a non-constant data structure.
// ----------------------------------------------------------------
// vector.json
// ----------------------------------------------------------------
{ "x": 1, "y": 2, "z": 3 }
// ----------------------------------------------------------------
// index.ts
// ----------------------------------------------------------------
import Vector from "./vector.json" with { type: "json" };
const { x, y, z } = Vector // actual = { x: number, y: number, z: number }
//
// expect = { x: 1, y: 2, z: 3 }
Information is lost regarding the literal values 1, 2, 3 (replaced by number). As information is lost on import, this makes type-mapping metadata encoded in the Json virtually impossible.
📃 Motivating Example
Import Json Schema
It should be possible for libraries to infer data structures from imported schematics like Json Schema. The minimum requirement is that the imported type be a literal representation of the data.
Ref: Inline Provided Type
import { type Static } from 'typebox'
// Vector3 is Provided Type
type Vector3 = Static<{ // type Vector3 = {
type: 'object', // x: number;
required: ['x', 'y', 'z'], // y: number;
properties: { // z: number;
x: { type: 'number' }, // }
y: { type: 'number' },
z: { type: 'number' }
}
}>
... but as import-attributes lose information for property values, the following isn't possible.
// ----------------------------------------------------------------
// vector.json
// ----------------------------------------------------------------
{
"type": "object",
"required": ["x", "y", "z"],
"properties": {
"x": { "type": "number" }, // observed as { type: string }
"y": { "type": "number" },
"z": { "type": "number" }
}
}
// ----------------------------------------------------------------
// index.ts
// ----------------------------------------------------------------
import { type Static } from 'typebox'
import Vector3 from './vector.json' with { type: 'json' }
// Vector3 is Provided Type | Incorrect
type Vector3 = Static<typeof Vector3> // type Vector3 = {
// x?: unknown;
// y?: unknown;
// z?: unknown;
// }
Import TypeScript via Text
If import-attributes with { type: 'text' } could also return literal string types. This would enable type safe DSL parsers to be developed for formats such as GraphQL, gRPC IDL or even TypeScript itself.
Ref: Parse TypeScript via Constant String Literal
import { type TScript, type Static } from 'typebox'
// Definition Parsing
type Math = TScript<{}, `
export type Vector2 = {
x: number
y: number
}
export type Vector3 = {
x: number
y: number
z: number
}
`>
// Vector3 | Vector2 as Provided Types
type Vector2 = Static<Math['Vector2']> // type Vector2 = {
// x: number;
// y: number;
// }
type Vector3 = Static<Math['Vector3']> // type Vector3 = {
// x: number;
// y: number;
// z: number;
// }
💻 Use Cases
What do you want to use this for?
At a high level:
- Use the TypeScript compiler to auto sync MCP protocol schematics loaded via URI (Deno). Example
- Use the TypeScript compiler to parse and infer WAT/WASM interface definitions Example
- Use the TypeScript compiler to parse foreign IDL formats like GraphQL Reference
- Use the TypeScript compiler to derive structures from WebGL/WebGPU (WGSL) compute programs Reference
What workarounds are you using in the meantime?
For inference, the current workaround is to encode structures in constant string literals.
// workable: would prefer to edit in '.graphql' and load via { type: 'text' } attribute
export const GraphQLTypes = `
type Starship {
id: ID!
name: String!
length(unit: LengthUnit = METER): Float
}
`
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Không có tệp nguồn, bài kiểm thử hoặc điểm vào của trình biên dịch nào được nêu; hãy bắt đầu bằng cách xác định cách xử lý thuộc tính import hiện có và các bài kiểm thử suy luận kiểu liên quan. So sánh hành vi được đề xuất là giữ nguyên literal cho các import JSON và văn bản với các issue được liên kết, đồng thời xác định các bài kiểm thử thiết lập những kiểu được suy luận theo dự định.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100