microsoft / microsoft/TypeScript

Preserve radix when transpiling number literals with separators

Aperta
#42,197 0 commenti 3 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

🔍 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]

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 tracciando il modo in cui TypeScript transpila i valori letterali numerici contenenti separatori per i target ES3/ES5 ed ES6–ES2020. Confronta esempi esadecimali, ottali e binari con e senza separatori, quindi aggiungi la copertura per l’output previsto che preserva la base numerica. Il lavoro è completo quando i target supportati mantengono la base numerica del sorgente, mentre il comportamento di ESNext rimane invariato.

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.