| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
TypeScript Bot (@typescript-bot) test this |
Sorry, something went wrong.
|
Heya Anders Hejlsberg (@ahejlsberg), I've started to run the parallelized community code test suite on this PR at 0bed057. You can monitor the build here. |
Sorry, something went wrong.
|
Heya Anders Hejlsberg (@ahejlsberg), I've started to run the perf test suite on this PR at 0bed057. You can monitor the build here. Update: The results are in! |
Sorry, something went wrong.
|
Heya Anders Hejlsberg (@ahejlsberg), I've started to run the parallelized Definitely Typed test suite on this PR at 0bed057. You can monitor the build here. |
Sorry, something went wrong.
|
Heya Anders Hejlsberg (@ahejlsberg), I've started to run the extended test suite on this PR at 0bed057. You can monitor the build here. |
Sorry, something went wrong.
|
Cooooooooooooooool!!!!!! Great work!!!!! |
Sorry, something went wrong.
|
Anders Hejlsberg (@ahejlsberg) Comparison Report - main..44730
System
Hosts
Scenarios
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sorry, something went wrong.
There was a problem hiding this comment.
Implementation-wise, this looks OK (is there a reason for specifically limiting the nesting analysis to two levels?); but I think the new tests have been elided, since you have a bunch of examples in the OP, but there's no new tests showing the functionality. (I'm interested in some with readonly props, both static and instance, since those don't have examples in your OP, but are referenced in some comments, even though later there's a check for exactly VariableDeclarations)
Sorry, something went wrong.
| break; | ||
| case SyntaxKind.CommaToken: | ||
| return narrowType(type, expr.right, assumeTrue); | ||
| // Ordinarily we won't see && and || expressions in control flow analysis because the Binder breaks those |
There was a problem hiding this comment.
What about ternary expressions?
Sorry, something went wrong.
There was a problem hiding this comment.
We narrow within the branches of ternary expressions, but not based on the result of ternary expressions, so I don't think there is anything to do there.
Sorry, something went wrong.
I'd like to have a fixed limit so we can avoid maintaining a stack of inlined symbols that would otherwise be needed (to catch accidentally recursive definitions). Two levels is rather arbitrary, could make it three or even five.
Yeah, haven't added tests yet. |
Sorry, something went wrong.
|
TypeScript Bot (@typescript-bot) pack this |
Sorry, something went wrong.
|
Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the tarball bundle task on this PR at 0bed057. You can monitor the build here. |
Sorry, something went wrong.
The isVariableDeclaration checks are all related to the declaration of the aliased condition or discriminant access, and are unrelated to how the referenced being narrowed was declared. But sure, we should have tests for all different kinds. |
Sorry, something went wrong.
|
Hey Ryan Cavanaugh (@RyanCavanaugh), I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so: {
"devDependencies": {
"typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/105495/artifacts?artifactName=tgz&fileId=94FC84B182999B7D6DCA47117848030F812A9A364841F767F760CB8D5DBAAF0402&fileName=/typescript-4.4.0-insiders.20210624.tgz"
}
}
and then running npm install. There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@4.4.0-pr-44730-11".; |
Sorry, something went wrong.
|
Is it possible to also add narrowing for constants from same destructurization? function test(data: { type: "num", value: number } | { type: "str", value: string }) {
const { type, value } = data;
if (type === "num") {
value.toFixed(); // currently error
data.value.toFixed(); // ok
} else {
value.charAt(0); // currently error
data.value.charAt(0); // ok
}
}UPD. As mentioned in edit I've missed - maybe later, but not in this PR. |
Sorry, something went wrong.
|
Anders Hejlsberg (@ahejlsberg), would that be possible to extend this for classes as well, e.g.: class T {
s = "";
}
function f1(x: unknown) {
const isT = x instanceof T;
if (isT) {
x.s; // Ok
}
}Also, maybe to include some other type checks, e.g., const isString = x?.constructor === String, which works for both primitive string values and instances of String class. |
Sorry, something went wrong.
|
Sorry, something went wrong.
Very cool! Interestingly, I can use object instead of unknown and it still works :) |
Sorry, something went wrong.
Should I take this mean narrowing won't work on non-readonly properties? That limits the utility of this IMO; I pretty much never use the readonly keyword in my interfaces. |
Sorry, something went wrong.
There was a problem hiding this comment.
It would still be nice to have tests documenting our behavior for other const-ish variable types (eg, static read-only fields) which alias a guard, if only to say we don't currently support it.
Sorry, something went wrong.
Correct. For example, the following fails if data isn't marked readonly: function test(obj: { readonly data: string | number }) {
const isString = typeof obj.data === 'string';
if (isString) {
let s: string = obj.data;
}
}The rationale here is that we can't know for certain that data hasn't been modified since the condition was evaluated. We'd effectively have to check that there are no assignments to obj.data between the declaration of isString and the reference to isString in the if statement. This adds complexity and could be prohibitively expensive. It does of course work if you copy the mutable property into a const local: function test(obj: { data: string | number }) {
const { data } = obj;
const isString = typeof data === 'string';
if (isString) {
let s: string = data;
}
} |
Sorry, something went wrong.
Thanks Joe Calzaretta (@jcalz) ! Correlated unions sound cool! Anders Hejlsberg (@ahejlsberg) would this be the correct issue to watch for this feature of "coupling" between destructured properties / elements? |
Sorry, something went wrong.
|
Anders Hejlsberg (@ahejlsberg) That would be to prevent that kind of weird behaviour ! |
Sorry, something went wrong.
| case SyntaxKind.AmpersandAmpersandToken: | ||
| return assumeTrue ? | ||
| narrowType(narrowType(type, expr.left, /*assumeTrue*/ true), expr.right, /*assumeTrue*/ true) : | ||
| getUnionType([narrowType(type, expr.left, /*assumeTrue*/ false), narrowType(type, expr.right, /*assumeTrue*/ false)]); |
There was a problem hiding this comment.
When assumeTrue is false, theoretically expr.right is reachable if and only if expr.left is truthy?
-getUnionType([narrowType(type, expr.left, /*assumeTrue*/ false), narrowType(type, expr.right, /*assumeTrue*/ false)])
+getUnionType([narrowType(type, expr.left, /*assumeTrue*/ false), narrowType(narrowType(type, expr.left, /*assumeTrue*/ true), expr.right, /*assumeTrue*/ false)])
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
With this PR we support control flow analysis of conditional expressions and discriminant property accesses indirectly referenced through const variables. For example:
Intuitively, indirect references to conditional expressions or discriminant property accesses behave exactly as if the expressions were written in-line.
Narrowing through indirect references occurs only when the conditional expression or discriminant property access is declared in a const variable declaration with no type annotation, and the reference being narrowed is a const variable, a readonly property, or a parameter for which there are no assignments in the function body.
Some examples where narrowing does not occur (but it would had the indirectly referenced conditions been written in-line):
Up to five levels of indirection are analyzed in conditional expressions. For example, two levels of indirections are analyzed in the following code:
It is possible to mix indirect conditional expressions and directly specified conditions:
This PR fixes most of the long list of issues that were closed as duplicates of #12184, but not all. In particular, the pattern of destructuring a discriminant property and a payload property into two local variables and expecting a coupling between the two is not supported as the control flow analyzer doesn't "see" the connection. For example:
We may be able to support that pattern later, but likely not in this PR.
Fixes #12184.
Fixes #19421.
Fixes #19943.
Fixes #24865.
Fixes #26804.
Fixes #31037.
Fixes #31059.
Fixes #31291.
Fixes #31344.
Fixes #31870.
Fixes #33192.
Fixes #34535.
Fixes #35603.
Fixes #36510.
Fixes #37638.
Fixes #37855.
Fixes #39657.
Fixes #39996.
Fixes #40201.
Fixes #40341.
Fixes #41266.
Fixes #41870.
Fixes #43333.