microsoft / microsoft/TypeScript
`noUncheckedIndexedAccess` should forbid unsound `Record<string, string>` → `Record<"k", string>` coercion
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
🔎 Search Terms
coercion, noUncheckedIndexedAccess, Record, unsound
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
noUncheckedIndexedAccess
⏯ Playground Link
💻 Code
let a: Record<string, string> = {};
let b: Record<"k", string> = a; // unsound
let bk: string = b.k;
bk.toLowerCase(); // fails at runtime
🙁 Actual behavior
No TypeScript errors.
🙂 Expected behavior
By enabling noUncheckedIndexedAccess, I’ve opted in to stricter errors that prevent mistakes where a potentially undefined record element is assumed to be defined. The coercion from a: Record<string, string> to Record<"k", string> is such a mistake, since it assumes that a.k is defined. So TypeScript should forbid this coercion when noUncheckedIndexedAccess is enabled.
Additional information about the issue
noUncheckedIndexedAccess already causes TypeScript to correctly reject the equivalent unsound coercion Record<string, string> → {k: string}.
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
Reproduce the example in the linked TypeScript Playground with noUncheckedIndexedAccess enabled, then compare the Record<string, string> to Record<"k", string> case with the equivalent coercion to { k: string }, which is already rejected. Done means the unsound Record coercion is rejected under noUncheckedIndexedAccess without changing the expected behavior described in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100