microsoft / microsoft/TypeScript

Preserve radix when transpiling number literals with separators

Open
#42,197 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how TypeScript transpiles numeric literals containing separators for ES3/ES5 and ES6–ES2020 targets. Compare hexadecimal, octal, and binary examples with and without separators, then add coverage for the expected radix-preserving output. Done means supported targets retain the source radix while ESNext behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.