Enhance members of literal type like const array type (e.g. string literal types should have literal length property)
See original GitHub issueThe 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:
- Created 4 years ago
- Reactions:7
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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
🍺