microsoft / microsoft/TypeScript

Preserve radix when transpiling number literals with separators

Offen
#42,197 0 Kommentare 3 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

🔍 Search Terms

  • number literals
  • radix

✅ 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

Currently, number literals with underscore separators are transpiled into decimal literals regardless of the radix used in the TypeScript source code.
I suggest to preserve the original radix of number literals whenever the ES target version supports it.

📃 Motivating Example

Consider this TypeScript code:

const a  = 0xFFFFFFFF;
const a_ = 0xFFFF_FFFF;
const b  = 0o666;
const b_ = 0o06_66;
const c  = 0b01010101;
const c_ = 0b0101_0101;

When transpiled to ES3/ES5 - Playground Link:

// ES3/ES5 (actual)
"use strict";
var a = 0xFFFFFFFF;
var a_ = 4294967295;
var b = 438;
var b_ = 438;
var c = 85;
var c_ = 85;

When transpiled to ES6 to ES2020 - Playground Link:

// ES6/ES2020 (actual)
"use strict";
const a = 0xFFFFFFFF;
const a_ = 4294967295;
const b = 0o666;
const b_ = 438;
const c = 0b01010101;
const c_ = 85;

If the original radix were kept, the output would rather be:

// ES3/ES5 (expected)
"use strict";
var a = 0xFFFFFFFF;
var a_ = 0xFFFFFFFF;
var b = 438;
var b_ = 438;
var c = 85;
var c_ = 85;

or

// ES6-ES2020 (expected)
"use strict";
const a = 0xFFFFFFFF;
const a_ = 0xFFFFFFFF;
const b = 0o666;
const b_ = 0o666;
const c = 0b01010101;
const c_ = 0b01010101;

ESNext as a target is not affected, since all number literals are kept unchanged.

Also, bigint literals in ES2020 are not affected, since the presence of an underscore separator does not make any difference: hexadecimal bigint literals always transpile to hexadecimal, octal and binary literals always transpile to decimal, regardless of any separators. Other minor inconsistencies like the case of the hexadecimal digits A-F or a-f for transpiled numbers vs. bigints are out of the scope of this suggestion.

💻 Use Cases

Non-decimal literals can make the code more readable, this is the obvious use here. As an additional effect, this would remove the inconsistent special treatment of hexadecimal number literals with separators with respect to their bigint counterparts.

// TypeScript
[0x10ffff, 0x10_ffff, 0x10ffffn, 0x10_ffffn]
// JavaScript

// Current:
[0x10ffff, 1114111, 0x10ffffn, 0x10ffffn]
           ^^^^^^^

// Probably better:
[0x10ffff, 0x10ffff, 0x10ffffn, 0x10ffffn]

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 nachzuverfolgen, wie TypeScript numerische Literale mit Trennzeichen für die Ziele ES3/ES5 und ES6–ES2020 transpiliert. Vergleiche hexadezimale, oktale und binäre Beispiele mit und ohne Trennzeichen und füge anschließend Abdeckung für die erwartete Ausgabe mit beibehaltenem Zahlensystem hinzu. Erledigt ist die Aufgabe, wenn unterstützte Ziele die Zahlendarstellung der Quelle beibehalten und das Verhalten von ESNext unverändert bleibt.

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.