Closed
Description
Bug Report
π Search Terms
type assertion keyof
, "type string is not assignable"
,'string' as keyof
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type casting
β― Playground Link
Playground link with relevant code
π» Code
type Pirate = 'Luffy' | 'JackSparrow' | 'Blackbeard';
function logPirate(pirate: Pirate) { console.log(pirate); }
// Typechecks correctly
logPirate(({p: 'Luffy'} as {p: Pirate}).p);
logPirate(({p: 'not a pirate'} as {p: Pirate}).p);
// Doesn't complain, but doesn't actually check for the constraint
// Dangerous!
logPirate({p: 'Luffy' as Pirate}.p);
logPirate({p: 'not a pirate' as Pirate}.p);
π Actual behavior
Casting a dictionary field that canβt be correct raises no compiler error nor warnings.
π Expected behavior
Casting a dictionary field that canβt be correct raises a compiler error OR needlessly risky casts raise warnings.