Skip to content

Assertions involving values from dictionary literals aren't properly checkedย #51270

@Rafer45

Description

@Rafer45

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions