Skip to content
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
Prev Previous commit
Next Next commit
Split tests
  • Loading branch information
andrewbranch committed May 12, 2022
commit 73e58ea929e3004b9b8e4a9eb0acef6caf2f88bb
12 changes: 8 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21248,11 +21248,15 @@ namespace ts {
type;
}

function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined, contextNode?: Node) {
if (!isLiteralOfContextualType(type, contextualType) && !(contextNode && isLiteralOfContextualType(type, instantiateContextualType(contextualType, contextNode)))) {
type = getWidenedUniqueESSymbolType(getWidenedLiteralType(type));
function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined, node?: Node) {
if (isLiteralOfContextualType(type, contextualType)) {
return type;
}
return type;
const instantiatedContextualType = node && instantiateContextualType(contextualType, node) || contextualType;
if (instantiatedContextualType !== contextualType && isLiteralOfContextualType(type, instantiatedContextualType)) {
return type;
}
return getWidenedUniqueESSymbolType(getWidenedLiteralType(type));
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
}

function getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded(type: Type | undefined, contextualSignatureReturnType: Type | undefined, isAsync: boolean) {
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
declare function f<T>(): T;
const {} = f(); // error
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)),
})
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
Copy link
Member

@weswigham weswigham May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this repro in theory involves completions, should it have a fourslash variant?

31 changes: 2 additions & 29 deletions tests/cases/compiler/inferTupleFromBindingPattern.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,2 @@
declare function f2<T>(cb: () => T): T;
const [e1, e2, e3] = f2(() => [1, "hi", true]);

declare function f1<T>(): T;
const {} = f1(); // error
const { p1 } = f1(); // error

declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ???

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)),
funcB: (...p) => d(f.funcB(...p)),
funcC: (...p) => d(f.funcC(...p))
})
);
declare function f<T>(cb: () => T): T;
const [e1, e2, e3] = f(() => [1, "hi", true]);