{ [Symbol.toPrimitive]: () => Symbol }: support as index/property type
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 25/100
- issue の種類
- 機能追加
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- javascript, typescript
- 領域
- compilers
調査の方向性
まず、動機となる例における計算されたプロパティ名とインデックスタイプに関して報告された3つの型チェック診断を確認してください。[Symbol.toPrimitive] メソッドを持つ値が、インターフェース、プロパティ名、インデックス式で受け入れられ、要求された型チェックの動作が維持されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Suggestion
🔍 Search Terms
Symbol.toPrimitiveunique 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
- 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. - 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. - 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時間
- マージ済み PR(30日)
- 117
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/TypeScript のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
microsoft/TypeScript#64322 · コメント 2 件 · リアクション 1 件 · 担当者 2 名 ·
-
Possible Improvement
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
microsoft/TypeScript#64278 · コメント 1 件 · リアクション 1 件 ·
-
Docs
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
microsoft/TypeScript#64118 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
microsoft/TypeScript#64094 ·
-
Docs
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
microsoft/TypeScript#63959 · コメント 5 件 ·
microsoft/TypeScript の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
kubernetes-sigs/prow#953 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
caddyserver/caddy#8046 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
jaegertracing/jaeger#9588 ·