-
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 in control flow analysis #43183
Conversation
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at a0fbf5c. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the perf test suite on this PR at a0fbf5c. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at a0fbf5c. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at a0fbf5c. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the parallelized community code test suite on this PR at a0fbf5c. You can monitor the build here. |
@ahejlsberg Here they are:Comparison Report - master..43183
System
Hosts
Scenarios
Developer Information: |
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
@ahejlsberg Here they are:Comparison Report - master..43183
System
Hosts
Scenarios
Developer Information: |
src/compiler/checker.ts
Outdated
function getConstraintForReference(type: Type, reference: Identifier | ElementAccessExpression | PropertyAccessExpression | QualifiedName, checkMode: CheckMode | undefined) { | ||
// When the type of a reference is or contains an instantiable type with a union type constraint, and | ||
// when the reference is in a constraint position (where it is known we'll obtain the apparent type) or | ||
// has a contextual type containing no instantiables (meaning constraints will determine assignability), |
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.
// has a contextual type containing no instantiables (meaning constraints will determine assignability), | |
// has a contextual type containing no top-level instantiables (meaning constraints will determine assignability), |
or
// has a contextual type containing no instantiables (meaning constraints will determine assignability), | |
// has a contextual type containing no immediate instantiables (meaning constraints will determine assignability), |
@typescript-bot pack this |
Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at 1a695e9. You can monitor the build here. |
Hey @DanielRosenwasser, 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 |
@ahejlsberg might be worth adding in the following test cases - interface Box<T> {
item: T;
}
declare function isBox(x: any): x is Box<unknown>;
declare function isUndefined(x: unknown): x is undefined;
declare function unbox<T>(x: Box<T>): T;
function f1<T extends Box<T> | undefined>(x: T) {
if (isBox(x)) {
unbox(x);
}
}
function f2<T extends Box<T> | undefined>(x: T) {
if (!isUndefined(x)) {
unbox(x);
}
}
function f3<T extends Box<T> | undefined>(x: T) {
if (!isBox(x)) {
// error
unbox(x);
}
}
function f4<T extends Box<T> | undefined>(x: T) {
if (isUndefined(x)) {
// error
unbox(x);
}
} |
@DanielRosenwasser Will do. |
Performance looks to be unaffected. RWC test suite looks good too, only change is a couple of error messages that now show constraint types instead of generic types (as expected). |
TS 4.7 catches a class of error which 4.6 and earlier did not, courtesy of microsoft/TypeScript#43183: if the type passed does not implement `toString()`, it notices, because it no longer defaults to falling back to `{}`. Fixes #330
TS 4.7 catches a class of error which 4.6 and earlier did not, courtesy of microsoft/TypeScript#43183: if the type passed does not implement `toString()`, it notices, because it no longer defaults to falling back to `{}`. To resolve this, *loosen* the constraints on what is acceptable as input to `toString`, by making the implementation itself more robust. Doing so also fixes a bug with the output for `toString` when working with strings: previously if you had `Maybe("a string")`, the `toString` output would be `'Maybe(a string)'`; it is now `'Maybe("a string")'`. Fixes #330
TS 4.7 catches a class of error which 4.6 and earlier did not, courtesy of microsoft/TypeScript#43183: if the type passed does not implement `toString()`, it notices, because it no longer defaults to falling back to `{}`. To resolve this, *loosen* the constraints on what is acceptable as input to `toString`, by making the implementation itself more robust. Doing so also fixes a bug with the output for `toString` when working with strings: previously if you had `Maybe("a string")`, the `toString` output would be `'Maybe(a string)'`; it is now `'Maybe("a string")'`. Fixes #330
(cherry picked from commit 25adb1a) --- Original message: Add support for TypeScript 4.7 TS 4.7 catches a class of error which 4.6 and earlier did not, courtesy of microsoft/TypeScript#43183: if the type passed does not implement `toString()`, it notices, because it no longer defaults to falling back to `{}`. To resolve this, *loosen* the constraints on what is acceptable as input to `toString`, by making the implementation itself more robust. Doing so also fixes a bug with the output for `toString` when working with strings: previously if you had `Maybe("a string")`, the `toString` output would be `'Maybe(a string)'`; it is now `'Maybe("a string")'`. Fixes #330
In #15576 we improved control flow analysis for references of generic types with nullable constraints when those references occur in constraint positions (locations where it is known we'll fetch the constraint).
This PR expands the concept further. Specifically, when the type of a reference is or contains a generic type with a union type constraint, and when the reference is in a constraint position or has a contextual type containing no generic types (meaning constraints will determine assignability), we substitute constraints for all generic types in the type of the reference to give control flow analysis an opportunity to narrow it further. For example:
Above, in
x.length
we substitutestring | undefined
forT
in the type ofx
becausex
occurs in a constraint position. Similarly inreturn x
we substitutestring | undefined
forT
in the type ofx
becausex
has the contextual typestring
(due to the return type annotation on the containing function) which contains no generic types. Following substitution, control flow analysis is then able to narrowstring | undefined
tostring
based on the truthiness check in the containingif
statement.Some additional examples:
Fixes #13995.
Fixes #20375.
Fixes #42939.