Closed
Description
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
.