-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Improve narrowing of generic types constrained to unknown
#60816
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,8 +39,7 @@ genericConditionalConstrainedToUnknownNotAssignableToConcreteObject.ts(13,5): er | |
M extends keyof T | ||
>(a2: ReturnType<T[M]>) { | ||
if (isA(a2)) { | ||
// a2 is not narrowed | ||
a2.x // error, but should be ok | ||
Comment on lines
-42
to
-43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those were outdated comments, as we can see here - no errors are reported here (rightfully so). I took the liberty to fix those here |
||
a2.x // ok | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
narrowUnknownByTypePredicate.ts(41,11): error TS2322: Type 'T' is not assignable to type 'null | undefined'. | ||
|
||
|
||
==== narrowUnknownByTypePredicate.ts (1 errors) ==== | ||
declare function isNotNullish(value: unknown): value is {}; | ||
declare function isNullish(value: unknown): value is null | undefined; | ||
|
||
declare const value1: unknown; | ||
if (isNotNullish(value1)) { | ||
value1; | ||
} | ||
|
||
declare const value2: unknown; | ||
if (!isNotNullish(value2)) { | ||
value2; | ||
} | ||
|
||
declare const value3: unknown; | ||
if (isNullish(value3)) { | ||
value3; | ||
} | ||
|
||
declare const value4: unknown; | ||
if (!isNullish(value4)) { | ||
value4; | ||
} | ||
|
||
declare class A { foo: string; } | ||
declare function isA(value: unknown): value is A; | ||
|
||
declare const value5: unknown; | ||
if (isA(value5)) { | ||
value5; | ||
} | ||
|
||
declare const value6: unknown; | ||
if (!isA(value6)) { | ||
value6; | ||
} | ||
|
||
function fn1<T>(x: T): void { | ||
if (x != undefined) { | ||
const y: {} = x; // ok | ||
} else { | ||
const y: null | undefined = x; // ok | ||
~ | ||
!!! error TS2322: Type 'T' is not assignable to type 'null | undefined'. | ||
!!! related TS2208 narrowUnknownByTypePredicate.ts:37:14: This type parameter might need an `extends null | undefined` constraint. | ||
Comment on lines
+45
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one really shouldn't error and it should 100% behave like the other case added here. I could work on this further - but like, the simplest potential fix would be to... return |
||
} | ||
} | ||
|
||
function fn2<T extends unknown>(x: T): void { | ||
if (x != undefined) { | ||
const y: {} = x; // ok | ||
} else { | ||
const y: null | undefined = x; // ok | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are 2 other calls to
getNarrowableTypeForReference
that are now not accompanied by thisrecombineUnknownType
dance. Before landing this, I should recheck both to see if that's OK.One of them is in
checkReturnExpression
- @gabritto do you have an opinion about this? This fix improves this scenario:but since the returned type can't "escape" the return statement, it feels redundant to recombine that unknown type there