Open

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: #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;