8000 `unknown` should narrow to `{}` when non-null · Issue #48928 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

unknow 8000 n should narrow to {} when non-null #48928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
paulferaud opened this issue May 3, 2022 · 2 comments
Closed

unknown should narrow to {} when non-null #48928

paulferaud opened this issue May 3, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@paulferaud
Copy link

Bug Report

🔎 Search Terms

empty type, unknown, top type, unknown not null

🕗 Version & Regression Information

Starting 3.0 (when unknown was introduced) and until at least 4.6.2

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

⏯ Playground Link

Playground link with relevant code

💻 Code

let myUnknown: unknown = null
let myEmpty: {} = myUnknown ?? 5
if (myUnknown != null) {
    let myEmpty2: {} = myUnknown
}

🙁 Actual behavior

Type 'unknown' is not assignable to type '{}'. (2322)

🙂 Expected behavior

I believe narrowing should be happening. unkown is the top type for everything. {} is the top type for everything non-null non-undefined. Therefore, when an unknown is or'ed (|| or ??) with a non-null, or if checked not-null itself (if (myUnknown != null)), then the result should be narrowed to {}.

Extra context: I'm trying to wrap around JSON.parse to return {} when the result is null. I would have expected that:

JSON.parse(arg) ?? {}

Be of the type {}, but TypeScript is insisting that it's actually unknown.

@MartinJohns
Copy link
Contributor

Duplicate of #32347. Used search terms: narrow unknown in:title

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label May 3, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants
0