Repository: TypeScript
Issue: Preserve radix when transpiling number literals with separators
Labels: Suggestion,Awaiting More Feedback

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

* number literals
* radix

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-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:

```ts
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](https://www.typescriptlang.org/play?target=0&ts=4.2.0-dev.20210103#code/MYewdgzgLgBAhjGBeGAGAHgMWz7BuAKFEljgH1k0sczdDjoYAjRFVEANi-vEaYrYhUHMtyK9YwVmiaoAjPMVyeJGMAEylZJXiA):

```js
// 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](https://www.typescriptlang.org/play?target=2&ts=4.2.0-dev.20210103#code/MYewdgzgLgBAhjGBeGAGAHgMWz7BuAKFEljgH1k0sczdDjoYAjRFVEANi-vEaYrYhUHMtyK9YwVmiaoAjPMVyeJGMAEylZJXiA):

```js
// 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:

```js
// ES3/ES5 (expected)
"use strict";
var a = 0xFFFFFFFF;
var a_ = 0xFFFFFFFF;
var b = 438;
var b_ = 438;
var c = 85;
var c_ = 85;
```

or

```js
// 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.

```ts
// TypeScript
[0x10ffff, 0x10_ffff, 0x10ffffn, 0x10_ffffn]
```

```ts
// JavaScript

// Current:
[0x10ffff, 1114111, 0x10ffffn, 0x10ffffn]
           ^^^^^^^

// Probably better:
[0x10ffff, 0x10ffff, 0x10ffffn, 0x10ffffn]
```