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.

New numeric string type to accurately represent numeric indexes

See original GitHub issue


🔍 Search Terms

Object key type, numeric string type, index signature parameter type

✅ 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

A new type should be created to represent numbers in string or number format (a number can be used where this type is used, but the real type is a string representation of a numerical value).

This should be used in cases where object indexes are set to number to allow for number and string representation of said number to be used in indexing and to make it more accurate as the indexes are actually stored as strings even if the restriction is that the creation of a new index should be done with a number.

This will also help with a few other places where the type is indicated as a number, but the real type is a string.

📃 Motivating Example & 💻 Use Cases

Conciseness improvment:

const a: {[prop: number]: number} = {};

a[1] = 123;
// below doesn't currently work even though there is no difference and this means the exact same thing as `a[2] = 321;`
a['2'] = 321; // assuming this worked for the rest of the example below

let i: string = ''; // here to show what the type of `i` is currently
for (i in a) {
    console.log(a, a[i], typeof i);
    // above will outputs { "1": 123, "2": 321 }, 123, "string"
    // in the first iteration, note the difference between the type specified in the object index type (`number`) and the type it really is (`string`)

This would instead be something like this (name of type is not final, I’m sure there are better names this can have)

const a: {[prop: numstring]: number} = {};
// since old versions allow `number`, this will be an added possiblity to be more concise
// the type above will make it more obvious that it's a string representation of a number (avoids surprises)

a[1] = 123;
a['2'] = 321; // this would now be allowed as well

let i: numstring = '0'; // here to show what the type of `i` is (type compatible with string, no parsing needed between them)
// can also be `let i: numstring = 0` to auto-wrap numbers
for (i in a) {
    console.log(a, a[i], typeof i);
    // above will outputs: { "1": 123, "2": 321 }, 123, "string"
    // in the first iteration, "string" will be the typeof similarly to how all objects are just "object" with `typeof`

    // this should make it more specific as to what the type is and whether or not the type can be converted to a number

    // the following can also be made to work:
    if (i === '1') {...}

This will also be useful in a similar way when looping through an array as the indexes are also treated as strings.

And another benefit outside of this will be to allow for string representation of numbers to be better defined as such in general

Issue Analytics

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

github_iconTop GitHub Comments

ahejlsbergcommented, Jun 19, 2021

There is ongoing work to improve index signatures in #44512. That PR includes fixing index signatures with key type number to consistently apply to values assignable to type number as well as all string literal types with round-tripping numeric values. Round-tripping here means that a given string value s satisfies the check +s.toString() === s. So, the a['2'] = 321 assignment in your example above will be permitted once #44512 is merged.

We don’t have a predefined type to represent a string with a round-tripping numeric value, but the template literal type `${number}` comes awfully close. It allows any string that can successfully be parsed as a number. That includes non-round-tripping strings like '1.0', '0.00', but I don’t think the difference is important enough to merit a whole new type.

ahejlsbergcommented, Jun 19, 2021

I guess to be 100% correct, only integers should be usable as indices

Actually, there’s nothing in JavaScript’s automatic number-to-string conversion when indexing an object that requires integers. It’s true there are optimizations for arrays that only apply for integer values, but that’s really an orthogonal issue. The thing we’re wanting for check for string literals used with numeric index signatures is that obj[s] and obj[+s] access the same property–because if they don’t, then the numeric index signature shouldn’t apply.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Types and Formats – Data Analysis and Visualization in ...
Numeric data types include integers and floats. A floating point (known as a float) ... Text data type is known as Strings in...
Read more >
Number - JavaScript - MDN Web Docs - Mozilla
Chrome Edge Number Full support. Chrome1. Toggle history Full support. Edge12. Toggl... EPSILON Full support. Chrome34. Toggle history Full support. Edge12. Toggl... MAX_SAFE_INTEGER Full support. Chrome34....
Read more >
Numeric Types - MATLAB & Simulink - MathWorks
All numeric types support basic array operations, such as indexing, reshaping, ... MATLAB represents floating-point numbers in either double-precision or ...
Read more >
Built-in Types — Python 3.11.1 documentation
Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including hex, octal and binary ......
Read more >
Introduction to data types and field properties - Microsoft Support
Depending on the field size, the number can use exactly 1, 2, 4, 8, 12, or 16 bytes. Note: Text and Memo fields...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found