-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Unwrap NoInfer
types when narrowing
#58292
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f35684
discriminate `NoInfer`ed union types
Andarist e4a3a4c
unwrap `NoInfer` types early
Andarist 7b1ce0e
add test case
Andarist 6bd13aa
update baseline
Andarist 5017184
Merge remote-tracking branch 'origin/main' into fix/noinfer-flow-type
Andarist d1e4079
Move `NoInfer` unwrapping to `getNarrowableTypeForReference`
Andarist a3c5821
add an extra test case
Andarist 34a6d3d
Merge remote-tracking branch 'origin/main' into fix/noinfer-flow-type
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 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//// [tests/cases/compiler/narrowingNoInfer1.ts] //// | ||
|
||
=== narrowingNoInfer1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/58266 | ||
|
||
type TaggedA = { _tag: "a" }; | ||
>TaggedA : Symbol(TaggedA, Decl(narrowingNoInfer1.ts, 0, 0)) | ||
>_tag : Symbol(_tag, Decl(narrowingNoInfer1.ts, 2, 16)) | ||
|
||
type TaggedB = { _tag: "b" }; | ||
>TaggedB : Symbol(TaggedB, Decl(narrowingNoInfer1.ts, 2, 29)) | ||
>_tag : Symbol(_tag, Decl(narrowingNoInfer1.ts, 3, 16)) | ||
|
||
type TaggedUnion = TaggedA | TaggedB; | ||
>TaggedUnion : Symbol(TaggedUnion, Decl(narrowingNoInfer1.ts, 3, 29)) | ||
>TaggedA : Symbol(TaggedA, Decl(narrowingNoInfer1.ts, 0, 0)) | ||
>TaggedB : Symbol(TaggedB, Decl(narrowingNoInfer1.ts, 2, 29)) | ||
|
||
const m: { result: NoInfer<TaggedUnion> }[] = []; | ||
>m : Symbol(m, Decl(narrowingNoInfer1.ts, 7, 5)) | ||
>result : Symbol(result, Decl(narrowingNoInfer1.ts, 7, 10)) | ||
>NoInfer : Symbol(NoInfer, Decl(lib.es5.d.ts, --, --)) | ||
>TaggedUnion : Symbol(TaggedUnion, Decl(narrowingNoInfer1.ts, 3, 29)) | ||
|
||
function map<A, B>(items: readonly A[], f: (a: NoInfer<A>) => B) { | ||
>map : Symbol(map, Decl(narrowingNoInfer1.ts, 7, 49)) | ||
>A : Symbol(A, Decl(narrowingNoInfer1.ts, 9, 13)) | ||
>B : Symbol(B, Decl(narrowingNoInfer1.ts, 9, 15)) | ||
>items : Symbol(items, Decl(narrowingNoInfer1.ts, 9, 19)) | ||
>A : Symbol(A, Decl(narrowingNoInfer1.ts, 9, 13)) | ||
>f : Symbol(f, Decl(narrowingNoInfer1.ts, 9, 39)) | ||
>a : Symbol(a, Decl(narrowingNoInfer1.ts, 9, 44)) | ||
>NoInfer : Symbol(NoInfer, Decl(lib.es5.d.ts, --, --)) | ||
>A : Symbol(A, Decl(narrowingNoInfer1.ts, 9, 13)) | ||
>B : Symbol(B, Decl(narrowingNoInfer1.ts, 9, 15)) | ||
|
||
return items.map(f); | ||
>items.map : Symbol(ReadonlyArray.map, Decl(lib.es5.d.ts, --, --)) | ||
>items : Symbol(items, Decl(narrowingNoInfer1.ts, 9, 19)) | ||
>map : Symbol(ReadonlyArray.map, Decl(lib.es5.d.ts, --, --)) | ||
>f : Symbol(f, Decl(narrowingNoInfer1.ts, 9, 39)) | ||
} | ||
|
||
const something = map(m, (_) => | ||
>something : Symbol(something, Decl(narrowingNoInfer1.ts, 13, 5)) | ||
>map : Symbol(map, Decl(narrowingNoInfer1.ts, 7, 49)) | ||
>m : Symbol(m, Decl(narrowingNoInfer1.ts, 7, 5)) | ||
>_ : Symbol(_, Decl(narrowingNoInfer1.ts, 13, 26)) | ||
|
||
_.result._tag === "a" ? { ..._, result: _.result } : null, | ||
>_.result._tag : Symbol(_tag, Decl(narrowingNoInfer1.ts, 2, 16), Decl(narrowingNoInfer1.ts, 3, 16)) | ||
>_.result : Symbol(result, Decl(narrowingNoInfer1.ts, 7, 10)) | ||
>_ : Symbol(_, Decl(narrowingNoInfer1.ts, 13, 26)) | ||
>result : Symbol(result, Decl(narrowingNoInfer1.ts, 7, 10)) | ||
>_tag : Symbol(_tag, Decl(narrowingNoInfer1.ts, 2, 16), Decl(narrowingNoInfer1.ts, 3, 16)) | ||
>_ : Symbol(_, Decl(narrowingNoInfer1.ts, 13, 26)) | ||
>result : Symbol(result, Decl(narrowingNoInfer1.ts, 14, 33)) | ||
>_.result : Symbol(result, Decl(narrowingNoInfer1.ts, 7, 10)) | ||
>_ : Symbol(_, Decl(narrowingNoInfer1.ts, 13, 26)) | ||
>result : Symbol(result, Decl(narrowingNoInfer1.ts, 7, 10)) | ||
|
||
); | ||
|
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,98 @@ | ||
//// [tests/cases/compiler/narrowingNoInfer1.ts] //// | ||
|
||
=== narrowingNoInfer1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/58266 | ||
|
||
type TaggedA = { _tag: "a" }; | ||
>TaggedA : TaggedA | ||
> : ^^^^^^^ | ||
>_tag : "a" | ||
> : ^^^ | ||
|
||
type TaggedB = { _tag: "b" }; | ||
>TaggedB : TaggedB | ||
> : ^^^^^^^ | ||
>_tag : "b" | ||
> : ^^^ | ||
|
||
type TaggedUnion = TaggedA | TaggedB; | ||
>TaggedUnion : TaggedUnion | ||
> : ^^^^^^^^^^^ | ||
|
||
const m: { result: NoInfer<TaggedUnion> }[] = []; | ||
>m : { result: NoInfer<TaggedUnion>; }[] | ||
> : ^^^^^^^^^^ ^^^^^ | ||
>result : NoInfer<TaggedUnion> | ||
> : ^^^^^^^^^^^^^^^^^^^^ | ||
>[] : never[] | ||
> : ^^^^^^^ | ||
|
||
function map<A, B>(items: readonly A[], f: (a: NoInfer<A>) => B) { | ||
>map : <A, B>(items: readonly A[], f: (a: NoInfer<A>) => B) => B[] | ||
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^ | ||
>items : readonly A[] | ||
> : ^^^^^^^^^^^^ | ||
>f : (a: NoInfer<A>) => B | ||
> : ^ ^^ ^^^^^ | ||
>a : NoInfer<A> | ||
> : ^^^^^^^^^^ | ||
|
||
return items.map(f); | ||
>items.map(f) : B[] | ||
> : ^^^ | ||
>items.map : <U>(callbackfn: (value: A, index: number, array: readonly A[]) => U, thisArg?: any) => U[] | ||
> : ^^^^ ^^^ ^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^ | ||
>items : readonly A[] | ||
> : ^^^^^^^^^^^^ | ||
>map : <U>(callbackfn: (value: A, index: number, array: readonly A[]) => U, thisArg?: any) => U[] | ||
> : ^^^^ ^^^ ^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^ | ||
>f : (a: NoInfer<A>) => B | ||
> : ^ ^^ ^^^^^^ | ||
} | ||
|
||
const something = map(m, (_) => | ||
>something : ({ result: TaggedA; } | null)[] | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>map(m, (_) => _.result._tag === "a" ? { ..._, result: _.result } : null,) : ({ result: TaggedA; } | null)[] | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>map : <A, B>(items: readonly A[], f: (a: NoInfer<A>) => B) => B[] | ||
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^ | ||
>m : { result: NoInfer<TaggedUnion>; }[] | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>(_) => _.result._tag === "a" ? { ..._, result: _.result } : null : (_: NoInfer<{ result: NoInfer<TaggedUnion>; }>) => { result: TaggedA; } | null | ||
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>_ : NoInfer<{ result: NoInfer<TaggedUnion>; }> | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
_.result._tag === "a" ? { ..._, result: _.result } : null, | ||
>_.result._tag === "a" ? { ..._, result: _.result } : null : { result: TaggedA; } | null | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>_.result._tag === "a" : boolean | ||
> : ^^^^^^^ | ||
>_.result._tag : "a" | "b" | ||
> : ^^^^^^^^^ | ||
>_.result : TaggedUnion | ||
> : ^^^^^^^^^^^ | ||
>_ : { result: NoInfer<TaggedUnion>; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>result : TaggedUnion | ||
> : ^^^^^^^^^^^ | ||
>_tag : "a" | "b" | ||
> : ^^^^^^^^^ | ||
>"a" : "a" | ||
> : ^^^ | ||
>{ ..._, result: _.result } : { result: TaggedA; } | ||
> : ^^^^^^^^^^^^^^^^^^^^ | ||
>_ : { result: NoInfer<TaggedUnion>; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>result : TaggedA | ||
> : ^^^^^^^ | ||
>_.result : TaggedA | ||
> : ^^^^^^^ | ||
>_ : { result: NoInfer<TaggedUnion>; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>result : TaggedA | ||
> : ^^^^^^^ | ||
|
||
); | ||
|
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,19 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/58266 | ||
|
||
type TaggedA = { _tag: "a" }; | ||
type TaggedB = { _tag: "b" }; | ||
|
||
type TaggedUnion = TaggedA | TaggedB; | ||
|
||
const m: { result: NoInfer<TaggedUnion> }[] = []; | ||
|
||
function map<A, B>(items: readonly A[], f: (a: NoInfer<A>) => B) { | ||
return items.map(f); | ||
} | ||
|
||
const something = map(m, (_) => | ||
_.result._tag === "a" ? { ..._, result: _.result } : null, | ||
); |
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.
My first instinct was that I should re-wrap the result of narrowing with
NoInfer
but quoting the originalNoInfer
PR:So I don't see a reason why that would be necessary. I also tested something like this (TS playground) to see if this would be needed and I don't think it is:
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.
I agree it's fine to erase
NoInfer
in variable, parameter, and property references, but I think it would be simpler and more consistent to do it first thing in thegetNarrowableTypeForReference
function. I think that's all you'd need.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.
moved it to
getNarrowableTypeForReference
, works like a charm - thanks!