microsoft / microsoft/TypeScript
Support type inference for Symbol keys on function
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
🔍 Search Terms
symbol index closure function ts7022
✅ 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
Typescript doesn't correctly infer the type when using a symbol as the key when adding a property to a function type.
Here is an example:
const TestSymbol: unique symbol = Symbol();
const c = () => {
return 'Hello, world!';
};
c['testProp'] = ['Hello']; // String property is correctly inferred to be of the type string[]
c[TestSymbol] = ['Hello']; // Typescript complains about the type being implicitly any.
c[TestSymbol] = ['Hello'] as string[] // This works as a workaround for this limitation in typescript.
The full error from the language server:
c[TestSymbol] implicitly has type 'any' because it does not have a type annotation
and is referenced directly or indirectly in its own initializer. (tsserver 7022)
Using a symbol this way works without any issues in plain javascript. I've tested Firefox, Chromium, Node and Bun and all these runtimes handle using symbols as property keys on a function without any issue.
It is very limiting not being able to use symbols naturally in typescript the same way they work in javascript. It would be great if typescript could infer the type of the property when using a symbol as the key the same way it does when using a string for the key.
📃 Motivating Example
I've not written any blog posts about this.
💻 Use Cases
- What do you want to use this for? I'm building some infrastructure code where I want to append some information to various objects. Using symbols removes the risk of key collisions that can occur when using strings.
- What shortcomings exist with current approaches? Using strings instead of symbols or manually casting to the expected type can lead to unexpected collisions and type errors.
- What workarounds are you using in the meantime? Strings and manually casting to the expected type.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das Beispiel für eine Funktionseigenschaft und die zugehörige tsserver-Diagnose 7022 zu reproduzieren. Es wird keine Quellcodedatei oder kein Test genannt, daher verfolge die Behandlung berechneter Symbol-Eigenschaften durch den Compiler sowie verwandter Inferenzfälle. Erledigt ist die Aufgabe, wenn die Zuweisung mit Symbolschlüssel ihren Eigenschaftstyp ohne explizite Annotation inferiert, während das bestehende Verhalten für String-Schlüssel unverändert bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100