Closed
Description
π Search Terms
{} undefined null predicate unknown narrowing
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing
β― Playground Link
π» Code
declare function isNotNullish(value: unknown): value is {};
declare function isNullish(value: unknown): value is null | undefined;
declare const value: unknown;
if (isNotNullish(value)) {
value;
// ^?
}
if (!isNotNullish(value)) {
value;
// ^?
}
if (isNullish(value)) {
value;
// ^?
}
if (!isNullish(value)) {
value;
// ^?
}
π Actual behavior
value
only narrows when the type predicate is called directly, not when negated with a !
.
The narrowed types of value
in that example are:
isNotNullish(value)
:{}
!isNotNullish(value)
:unknown
βisNullish(value)
:null undefined
!isNullish(value)
:unknown
β
π Expected behavior
The narrowed types of value
should be:
isNotNullish(value)
:{}
!isNotNullish(value)
:null | undefined
isNullish(value)
:null undefined
!isNullish(value)
:{}
Additional information about the issue
No response
Activity
unknown
narrowing by negated type predicates #60795Andarist commentedon Dec 17, 2024
Using
unknown
is so 2024: TS playgroundJoshuaKGoldberg commentedon Dec 18, 2024
What a ridiculously fast turnaround time. β€οΈ