Microsoft/TypeScript

Infer from usages should not widen literal types

Open

#52.991 aperta il 27 feb 2023

Vedi su GitHub
 (0 commenti) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
Domain: LS: RefactoringsExperience EnhancementHelp WantedSuggestion

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

Bug Report

🔎 Search Terms

string literal to string, infer type from usages, code fix, quickfix

⏯ Playground Link

Playground link with relevant code

💻 Code

let a

a = "foo" as "foo" | "bar"

let b

b = getB()

checkB(b)

function checkB(toCheck: "foo" | "bar") {
}

function getB(): "foo" | "bar" {
    return "foo"
}

🙁 Actual behavior

After running infer from usages quickfix, you get string type in a and b positions

🙂 Expected behavior

Should get inffered type "foo"

I think in case of as and satisfies clauses the type should be just copied literally?

As you can see from example this is common case for string enums, like when some function expects some string enum instead of just string, but this information gets lost

Guida contributor