-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve .origin
preservation in getNarrowedTypeWorker
#61589
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
Draft
Andarist
wants to merge
9
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/getNarrowedTypeWorker-origin-preservation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,751
−33
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d3fe0a
Improve `.origin` preservation in `getNarrowedTypeWorker`
Andarist 2eecb4d
bring back discriminant logic
Andarist 83bab0d
Revert "bring back discriminant logic"
Andarist 40ebaee
brign back comments
Andarist 2097c67
Bring the discriminant logic back in a different form
Andarist 5c8de5d
add comment
Andarist f3f4f5b
fixed narrowing by a type without a discriminant
Andarist 9850ba6
fix an extra case when the union has duplicated constituents with the…
Andarist 29ae00a
try to optimize
Andarist 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 hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/narrowingUnionByUnionCandidate1.js
This file contains hidden or 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,44 @@ | ||
//// [tests/cases/compiler/narrowingUnionByUnionCandidate1.ts] //// | ||
|
||
//// [narrowingUnionByUnionCandidate1.ts] | ||
// https://github.com/microsoft/TypeScript/issues/61581 | ||
|
||
type Result<A, E> = | ||
| { | ||
readonly _tag: "Ok"; | ||
readonly value: A; | ||
} | ||
| { | ||
readonly _tag: "Fail"; | ||
readonly error: E; | ||
}; | ||
|
||
declare const isResult: (u: unknown) => u is Result<any, any>; | ||
|
||
// return type: Result<A, E> | "ok" | ||
export const fn = <A, E>(inp: Result<A, E> | string) => | ||
isResult(inp) ? inp : "ok"; | ||
|
||
|
||
//// [narrowingUnionByUnionCandidate1.js] | ||
"use strict"; | ||
// https://github.com/microsoft/TypeScript/issues/61581 | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fn = void 0; | ||
// return type: Result<A, E> | "ok" | ||
var fn = function (inp) { | ||
return isResult(inp) ? inp : "ok"; | ||
}; | ||
exports.fn = fn; | ||
|
||
|
||
//// [narrowingUnionByUnionCandidate1.d.ts] | ||
type Result<A, E> = { | ||
readonly _tag: "Ok"; | ||
readonly value: A; | ||
} | { | ||
readonly _tag: "Fail"; | ||
readonly error: E; | ||
}; | ||
export declare const fn: <A, E>(inp: Result<A, E> | string) => Result<A, E> | "ok"; | ||
export {}; |
49 changes: 49 additions & 0 deletions
49
tests/baselines/reference/narrowingUnionByUnionCandidate1.symbols
This file contains hidden or 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,49 @@ | ||
//// [tests/cases/compiler/narrowingUnionByUnionCandidate1.ts] //// | ||
|
||
=== narrowingUnionByUnionCandidate1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/61581 | ||
|
||
type Result<A, E> = | ||
>Result : Symbol(Result, Decl(narrowingUnionByUnionCandidate1.ts, 0, 0)) | ||
>A : Symbol(A, Decl(narrowingUnionByUnionCandidate1.ts, 2, 12)) | ||
>E : Symbol(E, Decl(narrowingUnionByUnionCandidate1.ts, 2, 14)) | ||
|
||
| { | ||
readonly _tag: "Ok"; | ||
>_tag : Symbol(_tag, Decl(narrowingUnionByUnionCandidate1.ts, 3, 5)) | ||
|
||
readonly value: A; | ||
>value : Symbol(value, Decl(narrowingUnionByUnionCandidate1.ts, 4, 26)) | ||
>A : Symbol(A, Decl(narrowingUnionByUnionCandidate1.ts, 2, 12)) | ||
} | ||
| { | ||
readonly _tag: "Fail"; | ||
>_tag : Symbol(_tag, Decl(narrowingUnionByUnionCandidate1.ts, 7, 5)) | ||
|
||
readonly error: E; | ||
>error : Symbol(error, Decl(narrowingUnionByUnionCandidate1.ts, 8, 28)) | ||
>E : Symbol(E, Decl(narrowingUnionByUnionCandidate1.ts, 2, 14)) | ||
|
||
}; | ||
|
||
declare const isResult: (u: unknown) => u is Result<any, any>; | ||
>isResult : Symbol(isResult, Decl(narrowingUnionByUnionCandidate1.ts, 12, 13)) | ||
>u : Symbol(u, Decl(narrowingUnionByUnionCandidate1.ts, 12, 25)) | ||
>u : Symbol(u, Decl(narrowingUnionByUnionCandidate1.ts, 12, 25)) | ||
>Result : Symbol(Result, Decl(narrowingUnionByUnionCandidate1.ts, 0, 0)) | ||
|
||
// return type: Result<A, E> | "ok" | ||
export const fn = <A, E>(inp: Result<A, E> | string) => | ||
>fn : Symbol(fn, Decl(narrowingUnionByUnionCandidate1.ts, 15, 12)) | ||
>A : Symbol(A, Decl(narrowingUnionByUnionCandidate1.ts, 15, 19)) | ||
>E : Symbol(E, Decl(narrowingUnionByUnionCandidate1.ts, 15, 21)) | ||
>inp : Symbol(inp, Decl(narrowingUnionByUnionCandidate1.ts, 15, 25)) | ||
>Result : Symbol(Result, Decl(narrowingUnionByUnionCandidate1.ts, 0, 0)) | ||
>A : Symbol(A, Decl(narrowingUnionByUnionCandidate1.ts, 15, 19)) | ||
>E : Symbol(E, Decl(narrowingUnionByUnionCandidate1.ts, 15, 21)) | ||
|
||
isResult(inp) ? inp : "ok"; | ||
>isResult : Symbol(isResult, Decl(narrowingUnionByUnionCandidate1.ts, 12, 13)) | ||
>inp : Symbol(inp, Decl(narrowingUnionByUnionCandidate1.ts, 15, 25)) | ||
>inp : Symbol(inp, Decl(narrowingUnionByUnionCandidate1.ts, 15, 25)) | ||
|
58 changes: 58 additions & 0 deletions
58
tests/baselines/reference/narrowingUnionByUnionCandidate1.types
This file contains hidden or 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,58 @@ | ||
//// [tests/cases/compiler/narrowingUnionByUnionCandidate1.ts] //// | ||
|
||
=== narrowingUnionByUnionCandidate1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/61581 | ||
|
||
type Result<A, E> = | ||
>Result : Result<A, E> | ||
> : ^^^^^^^^^^^^ | ||
|
||
| { | ||
readonly _tag: "Ok"; | ||
>_tag : "Ok" | ||
> : ^^^^ | ||
|
||
readonly value: A; | ||
>value : A | ||
> : ^ | ||
} | ||
| { | ||
readonly _tag: "Fail"; | ||
>_tag : "Fail" | ||
> : ^^^^^^ | ||
|
||
readonly error: E; | ||
>error : E | ||
> : ^ | ||
|
||
}; | ||
|
||
declare const isResult: (u: unknown) => u is Result<any, any>; | ||
>isResult : (u: unknown) => u is Result<any, any> | ||
> : ^ ^^ ^^^^^ | ||
>u : unknown | ||
> : ^^^^^^^ | ||
|
||
// return type: Result<A, E> | "ok" | ||
export const fn = <A, E>(inp: Result<A, E> | string) => | ||
>fn : <A, E>(inp: Result<A, E> | string) => Result<A, E> | "ok" | ||
> : ^ ^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
><A, E>(inp: Result<A, E> | string) => isResult(inp) ? inp : "ok" : <A, E>(inp: Result<A, E> | string) => Result<A, E> | "ok" | ||
> : ^ ^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>inp : string | Result<A, E> | ||
> : ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
isResult(inp) ? inp : "ok"; | ||
>isResult(inp) ? inp : "ok" : Result<A, E> | "ok" | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
>isResult(inp) : boolean | ||
> : ^^^^^^^ | ||
>isResult : (u: unknown) => u is Result<any, any> | ||
> : ^ ^^ ^^^^^ | ||
>inp : string | Result<A, E> | ||
> : ^^^^^^^^^^^^^^^^^^^^^ | ||
>inp : Result<A, E> | ||
> : ^^^^^^^^^^^^ | ||
>"ok" : "ok" | ||
> : ^^^^ | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -557,8 +557,8 @@ if (isEmpty(test)) { | |
> : ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
test; // EmptyString | ||
>test : EmptyString | ||
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. Those changes are fine - it's the |
||
> : ^^^^^^^^^^^ | ||
>test : "" | null | undefined | ||
> : ^^^^^^^^^^^^^^^^^^^^^ | ||
} | ||
|
||
// Repro from #43825 | ||
|
Oops, something went wrong.
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.
This whole change aims to avoid any major functional changes.
Conceptually, it just "flips" the order of the 2 nested for loops. Before candidates were iterated and that iteration was iterating through types. With this change, it's the other way around - types are iterated and they have an inner loop through candidates. This required some changes to pull this off (like the introduction of
matchedCandidates
).But why this is even needed? The goal of this code is to narrow down a type by a candidate and to truly narrow down a type a
mapType
orfilterType
have to be used because they try to preserve.origin
information. By having the outer loop mapping throughcandidate
this "merely" produced a result related totype
but not one that would directly be derived from it -so the.origin
had very slim chances of surviving when narrowing by a union candidate