microsoft / microsoft/TypeScript
Symbol definition improvement
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
Search Terms
symbol in:title, symbol improvement, symbol description definition
Suggestion
By using more and more symbol in my projects, and to make debugging easier in development mode, I fill the optional description to Symbol. Then, on production, I remove all description during the bundling process:

So I was thinking that it could be really useful to have the same approach for TypeScript - playground:


Just imagine we don't have xsymbol<"test"> but the "native" symbol<"test"> or symbol("test") to be closer to DevTools.
Then, by extending this approach, I don't know if it could be helpful to increase the typing inference.
Given this code:
const foo = {
type: 'foo' as 'foo',
foo: ''
};
const bar = {
type: 'bar' as 'bar',
bar: ''
};
type FooBarAction = typeof foo | typeof bar;
function getType<T>(input: {type: T}): T {
return input.type;
}
function foobar(action: FooBarAction) {
switch (action.type) {
case getType(foo):
action.foo;
break;
case getType(bar):
action.bar;
break;
}
}

Here, according to the type property value, the action provide correctly foo property or bar property.
But, if I replace them by symbol:
const sFoo = {
type: Symbol('sfoo'),
sfoo: ''
};
const sBar = {
type: Symbol('sbar'),
sbar: ''
};
type SFooBarAction = typeof sFoo | typeof sBar;
function sfoobar(action: SFooBarAction) {
switch (action.type) {
case getType(sFoo):
action.sfoo;
break;
case getType(sBar):
action.sbar;
break;
}
}


Here TypeScript cannot resolve properly the correct definition of my action variable and keep saying that it get the SFooBarAction type, so only type property is available.
According to this PR #30196, I understand that working on symbol in the core system of TypeScript is not an easy thing, but probably it could be helpful if the optional description of Symbol as a definition descriptor to have the unique symbol in the definition. But definitely, as soon as we provide same description, we will face the same problem as today.
Use Cases
For the first part, everywhere when we use Symbol.
For the second part, as soon as unique value is required, like for Event Emitter, Action in Redux architecture, ...
Examples
See above on the Suggestion section
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.
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
Bắt đầu bằng cách tái hiện các ví dụ TypeScript Playground được liên kết, so sánh các trường hợp có discriminant là string với các trường hợp Symbol và xem xét PR #30196 liên quan. Công việc sẽ cần một thiết kế thống nhất cho các mô tả Symbol, các giá trị duy nhất và việc narrowing theo control flow, cùng với hành vi tương ứng của compiler và các bài kiểm thử thể hiện phép suy luận được yêu cầu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, 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
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100