microsoft / microsoft/TypeScript
Symbol definition improvement
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先复现链接的 TypeScript Playground 示例,对比字符串判别值的情况与 Symbol 的情况,并审阅相关的 PR #30196。这项工作需要为 Symbol 描述、唯一值和控制流缩窄确定一个明确的设计,同时提供相应的编译器行为以及展示所请求推断的测试。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100