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.

Enhance members of literal type like const array type (e.g. string literal types should have literal length property)

See original GitHub issue

The const array type, its principle members are also be the constant. When define a const array [number, string, boolean], its length type be 3. Also, when access to special index, the type also points the exact const type.

function const_array(): void
{
    let elements: [number, string, false] = [3, "something", false];
    let length: 3 = elements.length;

    let first: number = elements[0];
    let second: string = elements[1];
    let third: false = elements[2];
}

However, the literal type is not like the const array. When define a literal type “something”, its length be not 9 but number. Also, when access to a special index, the type is not a special literal type but a string type.

function literal(): void
{
    let word: "something" = "something" as const;
    let length: number = something.length; // not 9 but number

    let first: string = word[0]; // not "s" but string
    let second: string = word[1]; // not "o" but string
    let third: string = word[2]; // not "m" but string
}

What about enhancing the literal type to be like the const array type?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
sno2commented, Aug 16, 2022

Hello, for those looking for a more efficient solution, I’ve created a logarithmic implementation for anyone who is still facing this issue but can’t use the linear implementation due to the poor performance of it https://gist.github.com/sno2/7dac868ec6d11abb75250ce5e2b36041

Edit: hmm I will consider a logarithmic string indexer as well

1reaction
unionalcommented, Jun 28, 2022

Another use case: currently some type level arithmetics leverage the fact that array length produces number literal. This creates a performance issue. I am doing that in type-plus and during development I have to limit the digits to 3-4 so that the IDE is still performant.

With template literal type, if 'abc'['length'] gets the same treatment as [1,1,1]['length'], there might be a big performance gain there.

Another example of type level arithmetics: Implementing Arithmetic Within TypeScript’s Type System

But of course, the best solution is adding

type Add<A extends number, B extends number> = intrinsic
type Subtract<A extends number, B extends number> = intrinsic
type Increment<A extends number> = Add<A, 1>
type Decrement<A extends number> = Subtract<A, 1>

🍺

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Literal Types - TypeScript
There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an...
Read more >
Hope const number types can do simple arithmetic operations
Enhance members of literal type like const array type (e.g. string literal types should have literal length property) #34692.
Read more >
Get string literal types from Array of objects - Stack Overflow
You can do it if: The values in the array don't change at runtime (since type information is a compile-time-only thing with TypeScript);...
Read more >
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
Why Are Const Assertions a Gem in TypeScript?
A literal type precisely describes a value, such as a specific string, number, boolean, or enumeration member. Then the const assertion can help ......
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