microsoft / microsoft/TypeScript
Infer from usages should not widen literal types
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the behavior in the linked TypeScript Playground using the supplied code, then trace the “infer from usages” quickfix and its handling of assertions or satisfies clauses. Done means the inferred types for a and b preserve the literal "foo" rather than widening to string, with coverage for the shown case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100