-
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
In typeof x === object, type parameters extending unknown narrow like unknown itself #49091
In typeof x === object, type parameters extending unknown narrow like unknown itself #49091
Conversation
@typescript-bot pack this |
Heya @weswigham, I've started to run the extended test suite on this PR at dc319e7. You can monitor the build here. |
Heya @weswigham, I've started to run the parallelized Definitely Typed test suite on this PR at dc319e7. You can monitor the build here. |
Heya @weswigham, I've started to run the perf test suite on this PR at dc319e7. You can monitor the build here. Update: The results are in! |
Heya @weswigham, I've started to run the tarball bundle task on this PR at dc319e7. You can monitor the build here. |
Heya @weswigham, I've started to run the diff-based community code test suite on this PR at dc319e7. You can monitor the build here. Update: The results are in! |
// then it doesn't affect the `keyof` query, and we can unwrap the conditional and relate the unwrapped source and target. | ||
// There may be multiple stacked conditionals, such as `T extends null ? never : T extends undefined ? never : T : T`, so | ||
// we need to repeat the unwrapping process. | ||
while (isConditionalFilteringKeylessTypes(sourceType)) { |
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.
These keyof
relationship improvements aren't strictly speaking tied to the typeof x === "object"
narrowing change, but they do improve common uses of it (eg, narrowing within a loop and doing an index operation), so I think make sense to pair with it.
// Returns the String, Number, Boolean, StringLiteral, NumberLiteral, BooleanLiteral, Void, Undefined, or Null | ||
// flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns | ||
// no flags for all other types (including non-falsy literal types). | ||
function getFalsyFlags(type: Type): TypeFlags { | ||
return type.flags & TypeFlags.Union ? getFalsyFlagsOfTypes((type as UnionType).types) : | ||
type.flags & TypeFlags.Intersection ? reduceLeft(getApparentIntersectionTypes(type as IntersectionType), (memo, type) => memo | getFalsyFlags(type), 0 as TypeFacts) : |
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.
Small change to falsy flag calculation to pick up T & undefined
as falsy when it doesn't reduce to never
.
@weswigham |
Hey @weswigham, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
@weswigham Here they are:Comparison Report - main..49091
System
Hosts
Scenarios
Developer Information: |
} | ||
|
||
// #48468 but with an explicit constraint so as to not trigger the `{}` and unconstrained type parameter bug | ||
function deepEquals<T extends unknown>(a: T, b: T) { |
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.
Can you have a test for both the explicit and non-explicit constraint?
FWIW I have tests at #48576.
DT looks clean (some unrelated intl change is making it fail but otherwise clean), RWC has removed an error on an |
This mostly got folded into #49119. I'll extract the improvements to |
In addition, improves relating
keyof T
andkeyof NonNullable<T>
-like types (to allow the relation) so that the impacts of, eg, chaining atypeof x === "object"
and a truthiness check still produce compatible types.Should fix #48468 even after the
{}
and unconstrained type parameter change is back in.