Skip to content

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

Closed
@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