-
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
Limit type argument inference from binding patterns #49086
Merged
andrewbranch
merged 10 commits into
microsoft:main
from
andrewbranch:bug/binding-pattern-inference
May 24, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dc325fe
WIP
andrewbranch b00e6b5
Don’t widen literals based on bogus contextual type instantiation
andrewbranch 73e58ea
Split tests
andrewbranch 65f453f
Skip unnecessary inference pass
andrewbranch c923baf
Accept test baselines
andrewbranch 7e8c56c
Fix stray edit
andrewbranch 7dc1952
Fix type mapper combination
andrewbranch 96f2f52
Revert src/ of 7dc1952a82
andrewbranch 024bf7a
Make empty binding pattern provide no contextual type
andrewbranch 4537a79
Add missed baseline
andrewbranch 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
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.errors.txt
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,44 @@ | ||
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(2,7): error TS2571: Object is of type 'unknown'. | ||
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(3,9): error TS2339: Property 'p1' does not exist on type 'unknown'. | ||
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(4,7): error TS2461: Type 'unknown' is not an array type. | ||
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(4,7): error TS2571: Object is of type 'unknown'. | ||
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(5,7): error TS2461: Type 'unknown' is not an array type. | ||
|
||
|
||
==== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts (5 errors) ==== | ||
declare function f<T>(): T; | ||
const {} = f(); // error (only in strictNullChecks) | ||
~~ | ||
!!! error TS2571: Object is of type 'unknown'. | ||
const { p1 } = f(); // error | ||
~~ | ||
!!! error TS2339: Property 'p1' does not exist on type 'unknown'. | ||
const [] = f(); // error | ||
~~ | ||
!!! error TS2461: Type 'unknown' is not an array type. | ||
~~ | ||
!!! error TS2571: Object is of type 'unknown'. | ||
const [e1, e2] = f(); // error | ||
~~~~~~~~ | ||
!!! error TS2461: Type 'unknown' is not an array type. | ||
|
||
// Repro from #43605 | ||
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T }; | ||
type IFuncs = { readonly [key: string]: (...p: any) => void }; | ||
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void }; | ||
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U; | ||
const funcs1 = { | ||
funcA: (a: boolean): void => {}, | ||
funcB: (b: string, bb: string): void => {}, | ||
funcC: (c: number, cc: number, ccc: boolean): void => {}, | ||
}; | ||
type TFuncs1 = typeof funcs1; | ||
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T; | ||
const {} = useReduxDispatch1( | ||
(d, f) => ({ | ||
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable | ||
funcB: (...p) => d(f.funcB(...p)), | ||
funcC: (...p) => d(f.funcC(...p)), | ||
}) | ||
); | ||
|
61 changes: 61 additions & 0 deletions
61
tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.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,61 @@ | ||
//// [bindingPatternCannotBeOnlyInferenceSource.ts] | ||
declare function f<T>(): T; | ||
const {} = f(); // error (only in strictNullChecks) | ||
const { p1 } = f(); // error | ||
const [] = f(); // error | ||
const [e1, e2] = f(); // error | ||
|
||
// Repro from #43605 | ||
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T }; | ||
type IFuncs = { readonly [key: string]: (...p: any) => void }; | ||
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void }; | ||
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U; | ||
const funcs1 = { | ||
funcA: (a: boolean): void => {}, | ||
funcB: (b: string, bb: string): void => {}, | ||
funcC: (c: number, cc: number, ccc: boolean): void => {}, | ||
}; | ||
type TFuncs1 = typeof funcs1; | ||
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T; | ||
const {} = useReduxDispatch1( | ||
(d, f) => ({ | ||
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable | ||
funcB: (...p) => d(f.funcB(...p)), | ||
funcC: (...p) => d(f.funcC(...p)), | ||
}) | ||
); | ||
|
||
|
||
//// [bindingPatternCannotBeOnlyInferenceSource.js] | ||
var _a = f(); // error (only in strictNullChecks) | ||
var p1 = f().p1; // error | ||
var _b = f(); // error | ||
var _c = f(), e1 = _c[0], e2 = _c[1]; // error | ||
var funcs1 = { | ||
funcA: function (a) { }, | ||
funcB: function (b, bb) { }, | ||
funcC: function (c, cc, ccc) { } | ||
}; | ||
var _d = useReduxDispatch1(function (d, f) { return ({ | ||
funcA: function () { | ||
var p = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
p[_i] = arguments[_i]; | ||
} | ||
return d(f.funcA.apply(f, p)); | ||
}, | ||
funcB: function () { | ||
var p = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
p[_i] = arguments[_i]; | ||
} | ||
return d(f.funcB.apply(f, p)); | ||
}, | ||
funcC: function () { | ||
var p = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
p[_i] = arguments[_i]; | ||
} | ||
return d(f.funcC.apply(f, p)); | ||
} | ||
}); }); |
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
if
statement now runs even when there are no inference candidates. Is that intended?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.
Yes, because the
returnMapper
pulls from a different inference context that may have candidates not present ininferenceContext
. (Also,returnMapper
is guaranteed to have candidates if it exists.)