microsoft / microsoft/TypeScript

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

Offen
#32,895 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die Deklarationen der NumberConstructor- und StringConstructor-Schnittstellen in den Bibliotheksdefinitionen von TypeScript zu finden und vorhandene Tests zur Prüfung von Konstruktorargumenten zu überprüfen. Bestätige die gewünschte Unterscheidung zwischen Number(Symbol()), new Number(Symbol()), String(Symbol()) und new String(Symbol()), und füge anschließend Tests hinzu, die die beabsichtigten Compile-Time-Diagnosen zeigen. Als erledigt gilt, dass Symbolwerte nur dort abgelehnt werden, wo das Issue dies vorgibt, ohne das ausgegebene JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.