microsoft / microsoft/TypeScript
Improve support for Symbol.toPrimitive
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Presently, TS limits index types to 'string', 'number', 'symbol', or 'any'. That's fine, but consider the following:
class Foo {
[Symbol.toPrimitive](): number {
return 1;
}
}
var x = ['a','b','c'];
var f = new Foo();
x[f];
We give the error an index expression argument must be of type ... on the array access.
Foo is for all intents and purposes a wrapper around the number 1 (close to new Number(1)), yet it cannot be used to index an array, since Foo itself is not of type number, string, symbol, or any. We could do extra type-checking to see if an object has a user-defined Symbol.toPrimitive and consider its return types when checking type-coercing operations (after all, if you've opted in to using Symbol.toPrimitve it's probably part of your goal to gain this behavior), such as indexing or mathematics operators.
Building on this, we currently don't support the coercion of Number or String classes to their respective primitive types (which are a specialization of the above issue but which applies to ES5 and ES3 output). Presently one can't do this:
function add(a: string, b: string): number {
var x = new Number(a);
var y = new Number(b);
return x + y;
}
add('10e4', '0x8');
Because since neither x nor y is the 'number' (or 'string') primitive type, the addition operation is not allowed. Though it's still valid js (and without the usage of the Number class, the addition would result in '10e40x8'!).
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con gli esempi di espressioni di indice e di addizione nell’issue, quindi segui le regole di controllo dei tipi per gli argomenti di indice e gli operandi aritmetici. Il lavoro è completato quando Symbol.toPrimitive e la coercizione dei wrapper Number/String vengono gestiti in modo coerente, senza consentire operazioni non valide.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100