Skip to content

Type casting a variable to its own type should preserve literal widening informationΒ #51723

Open
@ethanresnick

Description

@ethanresnick

Bug Report

πŸ”Ž Search Terms

literal widening typeof

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const foo = "fooey";  // widening
let id1 = foo; // widening 
let id2 = foo as typeof foo; // non-widening

id1 = "hello" // ok
id2 = "hello" // type error

πŸ™ Actual behavior

Assigning to id2 gives a type error, while assigning the same thing to id1 does not.

πŸ™‚ Expected behavior

There should be absolutely no difference between id1 and id2, because foo as typeof foo should be an identity cast/no-op.

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