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

Fixed generic inference when the contextual type is a signature with properties #52495

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32371,18 +32371,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

// If type has a single call signature and no other members, return that signature. Otherwise, return undefined.
function getSingleCallSignature(type: Type): Signature | undefined {
return getSingleSignature(type, SignatureKind.Call, /*allowMembers*/ false);
return getSingleSignature(type, SignatureKind.Call, /*allowMembersWithTypeVariables*/ false);
}

function getSingleCallOrConstructSignature(type: Type): Signature | undefined {
return getSingleSignature(type, SignatureKind.Call, /*allowMembers*/ false) ||
getSingleSignature(type, SignatureKind.Construct, /*allowMembers*/ false);
return getSingleSignature(type, SignatureKind.Call, /*allowMembersWithTypeVariables*/ false) ||
getSingleSignature(type, SignatureKind.Construct, /*allowMembersWithTypeVariables*/ false);
}

function getSingleSignature(type: Type, kind: SignatureKind, allowMembers: boolean): Signature | undefined {
function couldMembersContainTypeVariables(resolved: ResolvedType) {
return some(resolved.properties, sym => couldContainTypeVariables(getTypeOfSymbol(sym))) ||
some(resolved.indexInfos, info => couldContainTypeVariables(info.type));
}

function getSingleSignature(type: Type, kind: SignatureKind, allowMembersWithTypeVariables: boolean): Signature | undefined {
if (type.flags & TypeFlags.Object) {
const resolved = resolveStructuredTypeMembers(type as ObjectType);
if (allowMembers || resolved.properties.length === 0 && resolved.indexInfos.length === 0) {
// We could validate that the other members don't reference the type parameters we want to promote to the signature.
// That's pretty hard, so a reasonable proxy is checking that the other members of the type don't reference any type variables at all.
if (allowMembersWithTypeVariables || !couldMembersContainTypeVariables(resolved)) {
if (kind === SignatureKind.Call && resolved.callSignatures.length === 1 && resolved.constructSignatures.length === 0) {
return resolved.callSignatures[0];
}
Expand Down Expand Up @@ -37601,13 +37608,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function instantiateTypeWithSingleGenericCallSignature(node: Expression | MethodDeclaration | QualifiedName, type: Type, checkMode?: CheckMode) {
if (checkMode && checkMode & (CheckMode.Inferential | CheckMode.SkipGenericFunctions)) {
const callSignature = getSingleSignature(type, SignatureKind.Call, /*allowMembers*/ true);
const constructSignature = getSingleSignature(type, SignatureKind.Construct, /*allowMembers*/ true);
const callSignature = getSingleSignature(type, SignatureKind.Call, /*allowMembersWithTypeVariables*/ true);
const constructSignature = getSingleSignature(type, SignatureKind.Construct, /*allowMembersWithTypeVariables*/ true);
const signature = callSignature || constructSignature;
if (signature && signature.typeParameters) {
const contextualType = getApparentTypeOfContextualType(node as Expression, ContextFlags.NoConstraints);
if (contextualType) {
const contextualSignature = getSingleSignature(getNonNullableType(contextualType), callSignature ? SignatureKind.Call : SignatureKind.Construct, /*allowMembers*/ false);
const contextualSignature = getSingleSignature(getNonNullableType(contextualType), callSignature ? SignatureKind.Call : SignatureKind.Construct, /*allowMembersWithTypeVariables*/ false);
if (contextualSignature && !contextualSignature.typeParameters) {
if (checkMode & CheckMode.SkipGenericFunctions) {
skippedGenericFunction(node, checkMode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
genericInferenceContextualTypeAsSignatureWithProperties.ts(17,15): error TS2345: Argument of type '(arg: 'a' | 'b') => void' is not assignable to parameter of type '(arg: "a" | "b" | "c") => void'.
Types of parameters 'arg' and 'arg' are incompatible.
Type '"a" | "b" | "c"' is not assignable to type '"a" | "b"'.
Type '"c"' is not assignable to type '"a" | "b"'.


==== genericInferenceContextualTypeAsSignatureWithProperties.ts (1 errors) ====
// repro from #52262

const inner = <T extends 'a' | 'b' | 'c'>(cb: (arg: T) => void) => {};

interface FuncB<T> {
(arg: T): void;
x?: string;
};
const outerB = <T,>(func: FuncB<T>, arg: T) => {};
outerB(inner, (arg: 'a' | 'b') => {});

interface FuncC<T> {
(arg: T): void;
x?: T;
};
const outerC = <T,>(func: FuncC<T>, arg: T) => {};
outerC(inner, (arg: 'a' | 'b') => {}); // error
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(arg: 'a' | 'b') => void' is not assignable to parameter of type '(arg: "a" | "b" | "c") => void'.
!!! error TS2345: Types of parameters 'arg' and 'arg' are incompatible.
!!! error TS2345: Type '"a" | "b" | "c"' is not assignable to type '"a" | "b"'.
!!! error TS2345: Type '"c"' is not assignable to type '"a" | "b"'.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//// [tests/cases/compiler/genericInferenceContextualTypeAsSignatureWithProperties.ts] ////

=== genericInferenceContextualTypeAsSignatureWithProperties.ts ===
// repro from #52262

const inner = <T extends 'a' | 'b' | 'c'>(cb: (arg: T) => void) => {};
>inner : Symbol(inner, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 5))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 15))
>cb : Symbol(cb, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 42))
>arg : Symbol(arg, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 47))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 15))

interface FuncB<T> {
>FuncB : Symbol(FuncB, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 70))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 4, 16))

(arg: T): void;
>arg : Symbol(arg, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 5, 3))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 4, 16))

x?: string;
>x : Symbol(FuncB.x, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 5, 17))

};
const outerB = <T,>(func: FuncB<T>, arg: T) => {};
>outerB : Symbol(outerB, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 8, 5))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 8, 16))
>func : Symbol(func, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 8, 20))
>FuncB : Symbol(FuncB, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 70))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 8, 16))
>arg : Symbol(arg, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 8, 35))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 8, 16))

outerB(inner, (arg: 'a' | 'b') => {});
>outerB : Symbol(outerB, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 8, 5))
>inner : Symbol(inner, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 5))
>arg : Symbol(arg, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 9, 15))

interface FuncC<T> {
>FuncC : Symbol(FuncC, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 9, 38))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 11, 16))

(arg: T): void;
>arg : Symbol(arg, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 12, 3))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 11, 16))

x?: T;
>x : Symbol(FuncC.x, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 12, 17))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 11, 16))

};
const outerC = <T,>(func: FuncC<T>, arg: T) => {};
>outerC : Symbol(outerC, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 15, 5))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 15, 16))
>func : Symbol(func, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 15, 20))
>FuncC : Symbol(FuncC, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 9, 38))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 15, 16))
>arg : Symbol(arg, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 15, 35))
>T : Symbol(T, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 15, 16))

outerC(inner, (arg: 'a' | 'b') => {}); // error
>outerC : Symbol(outerC, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 15, 5))
>inner : Symbol(inner, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 2, 5))
>arg : Symbol(arg, Decl(genericInferenceContextualTypeAsSignatureWithProperties.ts, 16, 15))

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [tests/cases/compiler/genericInferenceContextualTypeAsSignatureWithProperties.ts] ////

=== genericInferenceContextualTypeAsSignatureWithProperties.ts ===
// repro from #52262

const inner = <T extends 'a' | 'b' | 'c'>(cb: (arg: T) => void) => {};
>inner : <T extends "a" | "b" | "c">(cb: (arg: T) => void) => void
><T extends 'a' | 'b' | 'c'>(cb: (arg: T) => void) => {} : <T extends "a" | "b" | "c">(cb: (arg: T) => void) => void
>cb : (arg: T) => void
>arg : T

interface FuncB<T> {
(arg: T): void;
>arg : T

x?: string;
>x : string | undefined

};
const outerB = <T,>(func: FuncB<T>, arg: T) => {};
>outerB : <T>(func: FuncB<T>, arg: T) => void
><T,>(func: FuncB<T>, arg: T) => {} : <T>(func: FuncB<T>, arg: T) => void
>func : FuncB<T>
>arg : T

outerB(inner, (arg: 'a' | 'b') => {});
>outerB(inner, (arg: 'a' | 'b') => {}) : void
>outerB : <T>(func: FuncB<T>, arg: T) => void
>inner : <T extends "a" | "b" | "c">(cb: (arg: T) => void) => void
>(arg: 'a' | 'b') => {} : (arg: 'a' | 'b') => void
>arg : "a" | "b"

interface FuncC<T> {
(arg: T): void;
>arg : T

x?: T;
>x : T | undefined

};
const outerC = <T,>(func: FuncC<T>, arg: T) => {};
>outerC : <T>(func: FuncC<T>, arg: T) => void
><T,>(func: FuncC<T>, arg: T) => {} : <T>(func: FuncC<T>, arg: T) => void
>func : FuncC<T>
>arg : T

outerC(inner, (arg: 'a' | 'b') => {}); // error
>outerC(inner, (arg: 'a' | 'b') => {}) : void
>outerC : <T>(func: FuncC<T>, arg: T) => void
>inner : <T extends "a" | "b" | "c">(cb: (arg: T) => void) => void
>(arg: 'a' | 'b') => {} : (arg: 'a' | 'b') => void
>arg : "a" | "b"

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import React from "react";
*/
const TabbedShowLayout = () => {
>TabbedShowLayout : React.SFC<{}>
>() => { return ( <div className="" key=""> ok </div> );} : { (): JSX.Element; defaultProps: Partial<{}> | undefined; }
>() => { return ( <div className="" key=""> ok </div> );} : { (): React.ReactElement<any> | null; defaultProps: Partial<{}> | undefined; }

return (
>( <div className="" key=""> ok </div> ) : JSX.Element
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/unionOfClassCalls.types
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ arr.reduce((acc: Array<string>, a: number | string, index: number) => {

arr.forEach((a: number | string, index: number) => {
>arr.forEach((a: number | string, index: number) => { return index}) : void
>arr.forEach : ((callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void) | ((callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void)
>arr.forEach : ((callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void) | ((callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void)
>arr : number[] | string[]
>forEach : ((callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void) | ((callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void)
>forEach : ((callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void) | ((callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void)
>(a: number | string, index: number) => { return index} : (a: number | string, index: number) => number
>a : string | number
>index : number
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @strict: true
// @noEmit: true

// repro from #52262

const inner = <T extends 'a' | 'b' | 'c'>(cb: (arg: T) => void) => {};

interface FuncB<T> {
(arg: T): void;
x?: string;
};
const outerB = <T,>(func: FuncB<T>, arg: T) => {};
outerB(inner, (arg: 'a' | 'b') => {});

interface FuncC<T> {
(arg: T): void;
x?: T;
};
const outerC = <T,>(func: FuncC<T>, arg: T) => {};
outerC(inner, (arg: 'a' | 'b') => {}); // error