microsoft / microsoft/TypeScript

`unknown && false` is too loosely typed

Open
#53,352 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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 Falsy could be more specific than number. (#28682)
  • Falsy/Truthy were built in types.
  • HTMLAllCollection variant could be omitted as a legacy value since document.all is deprecated and it's otherwise the case that objects are not falsy.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.