microsoft / microsoft/TypeScript
`unknown && false` is too loosely typed
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
The following code should type-check: https://www.typescriptlang.org/play?#code/FAFwngDgpgBAYgQwDYGcwwLwwHYFclIwA+Mu2AJlAGYCW2U5xMVyKsJeAtgEZQBOTAAzYmAIlFMAEgBUAsgBkAggQDCAewJQAxiBprswYFTI69IqmrUAKAB4AuUtgDW2NQHdsASgeJU6AN7AMMEwfFAguHwiNjAAZLHMrFDAAL7AQA
🔎 Search Terms
unknown falsy false and &&
🕗 Version & Regression Information
Please keep and fill in the line that best applies:
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about unknown values and &&
⏯ Playground Link
Playground link with relevant code
💻 Code
type Falsy = null | undefined | false | number | 0n | "" | HTMLAllCollection
function foo(x: unknown): Falsy {
return x && false
}
🙁 Actual behavior
Fails to type-check
🙂 Expected behavior
Should type-check
This would be improved further if:
- NaN was its own type, so
Falsycould be more specific thannumber. (#28682) Falsy/Truthywere built in types.HTMLAllCollectionvariant could be omitted as a legacy value sincedocument.allis deprecated and it's otherwise the case that objects are not falsy.
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
Start with the linked TypeScript Playground example and reproduce the reported type-checking failure for function foo. Trace the compiler's handling of unknown, &&, and the Falsy return type; done means the example type-checks as expected.
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
- Clearly specified
- Newbie friendliness
- 42/100