question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preserve radix when pretty-printing numeric literal types

See original GitHub issue

Suggestion

🔍 Search Terms

numeric literal radix, non-decimal numeric literal types

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ 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

If a numeric literal type was defined explicitly with a non-decimal literal, it should be displayed as such in editor hovers and other places where it is pretty-printed.

📃 Motivating Example

Often, non-decimal literals are more-or-less self-documenting in a context:

declare const MASK: 0b1011; // it is obvious which bits are set
// vs
declare const MASK: 11; // wat?
declare const PERMISSIONS: 0o777; // unix file system permissions
// vs
declare const PERMISSIONS: 511; // wat?
declare const ARGB: 0x00ffff00; // yellow pixel color
// vs
declare const ARGB: 16776960; // wat?
declare const ASCII_CODE: 0x1b; // ASCII code for backspace
// vs
declare const ASCII_CODE: 27; // wat?

Ideally, the radix should carry over to the “autoincremented” enum members:

enum AsciiLetters {
  A = 0x61, B, C, D, E, F,  /* ... */, Z,
}
// AsciiLetters.E: 0x65

💻 Use Cases

Bit masks. Unix file system permissions. RGB colors. Hexadecimal ASCII/Unicode code points.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kingwlcommented, Aug 24, 2021

It’s might be a breaking change. Might should behind a flag?

1reaction
iliazeuscommented, Aug 23, 2021

I am concerned about declaration emit, for the same reasons I described above.

Funnily enough, when I test it on the playground, JS emit preserves the radix, but not the .D.TS emit:

https://www.typescriptlang.org/play?ts=4.4.0-beta#code/KYDwDg9gTgLgBAYwgOwM7wLIHkAiBROAXjgAYIB2SgbgFgAoIA

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preserve radix when transpiling number literals with separators
Currently, number literals with underscore separators are transpiled into decimal literals regardless of the radix used in the TypeScript source ...
Read more >
18. A Few FORMAT Recipes - gigamonkeys
To further complicate matters, FORMAT supports three quite different kinds of formatting: printing tables of data, pretty-printing s-expressions, ...
Read more >
Literals in Programming Languages
Design issues, types of literals, and problems with literals are illustrated. ... They allow radix (number base) 2, 8, or 16.
Read more >
Formatted Output (Guile Reference Manual) - GNU.org
The format function is a powerful way to print numbers, strings and other objects together with literal text under the control of a...
Read more >
TypeScript: error when using parseInt() on a number
parseInt (string , radix) The parseInt function produces an integer value dictated by interpretation of the contents of the string argument ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found