-
Couldn't load subscription status.
- Fork 13.1k
Use lib conditional types for type facts if possible #22348
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
Changes from all commits
988e5a4
cb855af
bf6029b
abc5684
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 |
|---|---|---|
|
|
@@ -3299,7 +3299,7 @@ Actual: ${stringify(fullActual)}`); | |
| return ts.createTextSpanFromRange(ranges[index]); | ||
| } | ||
| else { | ||
| this.raiseError("Supplied span index: " + index + " does not exist in range list of size: " + (ranges ? 0 : ranges.length)); | ||
| this.raiseError("Supplied span index: " + index + " does not exist in range list of size: " + (!ranges ? 0 : ranges.length)); | ||
|
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. 🤣 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. Right? Tfw the change finds a real error in the code. |
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //// [strictGenericNarrowingUsesNonFalsy.ts] | ||
| function f<T extends { x?: number }>(o: Readonly<T>) { | ||
| if (o.x) { | ||
| o.x.toExponential(); // Hover over 'x' shows number | ||
| const n: number = o.x; // Error. Hover over 'x' shows `T["x"]` | ||
| } | ||
| } | ||
|
|
||
|
|
||
| //// [strictGenericNarrowingUsesNonFalsy.js] | ||
| "use strict"; | ||
| function f(o) { | ||
| if (o.x) { | ||
| o.x.toExponential(); // Hover over 'x' shows number | ||
| var n = o.x; // Error. Hover over 'x' shows `T["x"]` | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| === tests/cases/compiler/strictGenericNarrowingUsesNonFalsy.ts === | ||
| function f<T extends { x?: number }>(o: Readonly<T>) { | ||
| >f : Symbol(f, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 0)) | ||
| >T : Symbol(T, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 11)) | ||
| >x : Symbol(x, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 22)) | ||
| >o : Symbol(o, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 37)) | ||
| >Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --)) | ||
| >T : Symbol(T, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 11)) | ||
|
|
||
| if (o.x) { | ||
| >o.x : Symbol(x, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 22)) | ||
| >o : Symbol(o, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 37)) | ||
| >x : Symbol(x, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 22)) | ||
|
|
||
| o.x.toExponential(); // Hover over 'x' shows number | ||
| >o.x.toExponential : Symbol(Number.toExponential, Decl(lib.d.ts, --, --)) | ||
| >o.x : Symbol(x, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 22)) | ||
| >o : Symbol(o, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 37)) | ||
| >x : Symbol(x, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 22)) | ||
| >toExponential : Symbol(Number.toExponential, Decl(lib.d.ts, --, --)) | ||
|
|
||
| const n: number = o.x; // Error. Hover over 'x' shows `T["x"]` | ||
| >n : Symbol(n, Decl(strictGenericNarrowingUsesNonFalsy.ts, 3, 13)) | ||
| >o.x : Symbol(x, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 22)) | ||
| >o : Symbol(o, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 37)) | ||
| >x : Symbol(x, Decl(strictGenericNarrowingUsesNonFalsy.ts, 0, 22)) | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| === tests/cases/compiler/strictGenericNarrowingUsesNonFalsy.ts === | ||
| function f<T extends { x?: number }>(o: Readonly<T>) { | ||
| >f : <T extends { x?: number | undefined; }>(o: Readonly<T>) => void | ||
| >T : T | ||
| >x : number | undefined | ||
| >o : Readonly<T> | ||
| >Readonly : Readonly<T> | ||
| >T : T | ||
|
|
||
| if (o.x) { | ||
| >o.x : T["x"] | ||
| >o : Readonly<T> | ||
| >x : T["x"] | ||
|
|
||
| o.x.toExponential(); // Hover over 'x' shows number | ||
| >o.x.toExponential() : string | ||
| >o.x.toExponential : (fractionDigits?: number | undefined) => string | ||
| >o.x : number | ||
| >o : Readonly<T> | ||
| >x : number | ||
| >toExponential : (fractionDigits?: number | undefined) => string | ||
|
|
||
| const n: number = o.x; // Error. Hover over 'x' shows `T["x"]` | ||
| >n : number | ||
| >o.x : TypeFacts.Truthy<T["x"]> | ||
| >o : Readonly<T> | ||
| >x : TypeFacts.Truthy<T["x"]> | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // @strict: true | ||
| function f<T extends { x?: number }>(o: Readonly<T>) { | ||
| if (o.x) { | ||
| o.x.toExponential(); // Hover over 'x' shows number | ||
| const n: number = o.x; // Error. Hover over 'x' shows `T["x"]` | ||
| } | ||
| } |
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.
What's this about?
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.
The
typeofnarrows toTKindin the true branch, but the false branch narrows toNENumber<TKind | Token<TKind>>without simplifying even though it obviously should (since generic conditionals don't get simplified when distributive), IIRC. Would have to look at it again to know for sure, though. Might not even repro after a merge - it's been a bit and we've iterated in conditional types quite a bit.