{ [Symbol.toPrimitive]: () => Symbol }: support as index/property type

未关闭
#43,428 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
停滞
领域
compilers

调研方向

首先检查动机示例中针对计算属性名和索引类型报告的三个类型检查诊断。完成的标准是:带有 [Symbol.toPrimitive] 方法的值在接口、属性名和索引表达式中都能被接受,同时保持所要求的类型检查行为。

由索引模型根据 Issue 内容生成。

描述

Awaiting More Feedback Suggestion

Suggestion

🔍 Search Terms

  • Symbol.toPrimitive
  • unique symbol

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

  1. Instead of receiving A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type. I want to be able to use types with [Symbol.toPrimitive] methods as properties in an interface.
  2. Instead of receiving A computed property name must be of type 'string', 'number', 'symbol', or 'any'. I want to be able to use a value with a [Symbol.toPrimitive] method as a property name.
  3. Instead of receiving Type 'SomeType' cannot be used as an index type. I want to be able to use a value with a [Symbol.toPrimitive] method as an index type.

📃 Motivating Example

class UniquePerson {
  #symbol = Symbol();
  constructor(readonly name: string) {}
  [Symbol.toPrimitive]() {
    return this.#symbol;
  }
}
const sherlock = new UniquePerson("Sherlock Holmes");
interface PersonToAddress {
  [sherlock]: string;
  // ^ A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
}
const personToAddress: PersonToAddress = {
  [sherlock]: "221B Baker Street",
  // ^ A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
};
personToAddress[sherlock]; //=> 221B Baker Street
               // ^ Type 'UniquePerson' cannot be used as an index type.

💻 Use Cases

I am working on a library that heavily uses Symbols to add extension function to other types while avoiding name conflicts (as Symbols are unique). Instead of passing around Symbols directly I want to pass around richer objects with more information but, when passed in as an index type, I want the [Symbol.toPrimitive] functions on my richer objects to be called and resolve to the appropriate Symbol. Currently I am exploring either not supporting TypeScript initially (which is not ideal and could inhibit adoption) or ditch the idea of passing around richer objects and use Symbols directly (but this greatly reduces the usability and usefulness of the library).

主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。