microsoft / microsoft/TypeScript

Disallow values of type 'symbol' from being passed to 'Number' function/constructor or 'String' constructor

Aperta
#32,895 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Suggestion

Change the NumberConstructor and StringConstructor interfaces to disallow passing values of type Symbol. Currently it accepts values of type any.

Attempting to call Number(Symbol()), new Number(Symbol()), or new String(Symbol()) all throw TypeErrors at runtime.

Note: String(Symbol()) is fine - only new String(Symbol()) will throw.

Use Cases

Any time a value of type any is passed to Number, there is a risk of that value being a symbol and throwing a TypeError.

Object keys are explicitly allowed to be of type symbol (as number | string | symbol). Passing an arbitrary object key to Number can throw a TypeError as well.

Examples

Checking if a value can be coerced to a number

I just ran into this issue when checking if an object key could be a valid array index. The following should be an error as it will throw if value is of type symbol.

function keyCanBeArrayIndex(value: string | number | symbol): boolean {
  return !Number.isNaN(Number(value));
}

Having this be invalid would have forced me to write something like:

function canBeArrayIndex(value: string | number | symbol): boolean {
  try {
    return !Number.isNaN(Number(value));
  } catch {
    return false;
  }
}

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia individuando le dichiarazioni delle interfacce NumberConstructor e StringConstructor nelle definizioni delle librerie di TypeScript e rivedi i test esistenti per il controllo degli argomenti dei costruttori. Conferma la distinzione desiderata tra Number(Symbol()), new Number(Symbol()), String(Symbol()) e new String(Symbol()), quindi aggiungi una copertura che mostri le diagnosi in fase di compilazione previste. Il lavoro è completato quando i valori symbol vengono rifiutati solo dove specificato dall’issue, senza modificare il JavaScript emesso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.