Skip to content

noUncheckedIndexedAccess should forbid unsound Record<string, string> β†’ Record<"k", string> coercionΒ #62796

@andersk

Description

@andersk

πŸ”Ž 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

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.9.3#code/DYUwLgBAhgXBBKIDGB7ATgEwDwGcxoEsA7AcwBoI9DSA+CAXggG8BfAbgChRIAjORVJiwAiANbCKVYiTqMobCAHpFEAK5EcKdRi7gIPUXCmkG+gHSjOBs2BQAZFAHcQaAMJQcIABQBKBcogAMygCYBxoSDR1MAIAWxAOIA

πŸ’» 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}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions