-
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
Use lib conditional types for type facts if possible #22348
Closed
weswigham
wants to merge
4
commits into
microsoft:main
from
weswigham:use-conditionals-while-narrowing
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
988e5a4
Use lib conditional types for type facts if possible
weswigham cb855af
Fix bug in checker that created zero length intersections, try constr…
weswigham bf6029b
Merge branch 'master' into use-conditionals-while-narrowing
weswigham abc5684
A break and a (maybe) bug
weswigham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/baselines/reference/strictGenericNarrowingUsesNonFalsy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]` | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/baselines/reference/strictGenericNarrowingUsesNonFalsy.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
|
30 changes: 30 additions & 0 deletions
30
tests/baselines/reference/strictGenericNarrowingUsesNonFalsy.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]> | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]` | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
typeof
narrows toTKind
in 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.