microsoft / microsoft/TypeScript
Strings do not have known keys associated with their indexes
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: Nightly
Code
{
const t: 't' = ("test" as const)[0];
}
{
const t: 't' = ([ 't', 'e', 's', 't' ] as const)[0];
}
Expected behavior:
TypeScript associated keys with values for string literals.
Actual behavior:
TypeScript gives all indexes into a string the type string.
TypeScript allows out of bounds indexes into string literals.
Related Issues: https://github.com/microsoft/TypeScript/pull/19846
Strings, especially string literals, ought to be comparable to an array of characters when indexed.
When accessing a string "foo" the type should be comparable to indexing readonly [ 'f', 'o', 'o' ], especially considering that JavaScript strings are completely immutable.
I doubt anyone would be against the lengths being known too, ex: const length: 4 = "test".length;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare string indexing with the readonly tuple example. The requested behavior is for literal string indexes to produce character literal types, reject out-of-bounds indexes, and potentially infer literal lengths; the issue names no source files or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100