Skip to content

fix(50858): parameters in function signatures inferred from default generic types sometimes not assigned instantiated types #50960

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

Closed
wants to merge 3 commits into from
Closed
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
47 changes: 36 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27532,12 +27532,31 @@ namespace ts {
function instantiateContextualType(contextualType: Type | undefined, node: Node, contextFlags: ContextFlags | undefined): Type | undefined {
if (contextualType && maybeTypeOfKind(contextualType, TypeFlags.Instantiable)) {
const inferenceContext = getInferenceContext(node);
// If no inferences have been made, nothing is gained from instantiating as type parameters
// would just be replaced with their defaults similar to the apparent type.
if (inferenceContext && contextFlags! & ContextFlags.Signature && some(inferenceContext.inferences, hasInferenceCandidates)) {
// For contextual signatures we incorporate all inferences made so far, e.g. from return
// types as well as arguments to the left in a function call.
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
if (inferenceContext && contextFlags! & ContextFlags.Signature) {
// If no inferences have been made, nothing is gained from instantiating as type parameters
// would just be replaced with their defaults similar to the apparent type.
if (some(inferenceContext.inferences, hasInferenceCandidates)) {
// For contextual signatures we incorporate all inferences made so far, e.g. from return
// types as well as arguments to the left in a function call.
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
}

const apparentType = mapType(contextualType, getApparentType, /*noReductions*/ true);

// If the apparent type is any, we cannot lose any information by instantiating,
// so we might as well try and possibly get a useful type
if (apparentType === anyType) {
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
}
Comment on lines +27546 to +27550
Copy link
Contributor

Choose a reason for hiding this comment

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

In what cases this is helpful? It's not totally clear to me what exactly the apparent type is but if I take into account the comment above the getApparentType then it should return the base constraint for type parameters.

Given that this PR introduces only tests that have a base constraint that is not any - I wonder, what existing test cases does this branch satisfy?

else if((apparentType.flags & TypeFlags.Union)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
else if((apparentType.flags & TypeFlags.Union)) {
else if ((apparentType.flags & TypeFlags.Union)) {

const signatureList = getContextualSignatureListFromApparentTypes((apparentType as UnionType).types, node as FunctionExpression | ArrowFunction | MethodDeclaration);

if (!signatureList) {
// At this point, the signature type is guaranteed to eventually collapse into any
// because it is a union of incompatible function signatures
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// because it is a union of incompatible function signatures
// because it is a union of incompatible function signatures or there is no signature at all

return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
}
}
}
if (inferenceContext?.returnMapper) {
// For other purposes (e.g. determining whether to produce literal types) we only
Expand Down Expand Up @@ -27953,8 +27972,17 @@ namespace ts {
if (!(type.flags & TypeFlags.Union)) {
return getContextualCallSignature(type, node);
}
let signatureList: Signature[] | undefined;
const types = (type as UnionType).types;
const signatureList = getContextualSignatureListFromApparentTypes(types, node);

// Result is union of signatures collected (return type is union of return types of this signature set)
if (signatureList) {
return signatureList.length === 1 ? signatureList[0] : createUnionSignature(signatureList[0], signatureList);
}
}

function getContextualSignatureListFromApparentTypes(types: Type[], node: FunctionExpression | ArrowFunction | MethodDeclaration): Signature[] | undefined {
let signatureList: Signature[] | undefined;
for (const current of types) {
const signature = getContextualCallSignature(current, node);
if (signature) {
Expand All @@ -27972,10 +28000,7 @@ namespace ts {
}
}
}
// Result is union of signatures collected (return type is union of return types of this signature set)
if (signatureList) {
return signatureList.length === 1 ? signatureList[0] : createUnionSignature(signatureList[0], signatureList);
}
return signatureList;
}

function checkSpreadExpression(node: SpreadElement, checkMode?: CheckMode): Type {
Expand Down
17 changes: 17 additions & 0 deletions tests/baselines/reference/genericInferenceDefaultTypeParameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [genericInferenceDefaultTypeParameter.ts]
type Type = {
a: (e: string) => void,
b: (e: number) => void,
}

function f1<T extends keyof Type = 'a'>(props: Type[T]): any {
return null
}

f1((event) => { })

//// [genericInferenceDefaultTypeParameter.js]
function f1(props) {
return null;
}
f1(function (event) { });
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/genericInferenceDefaultTypeParameter.ts ===
type Type = {
>Type : Symbol(Type, Decl(genericInferenceDefaultTypeParameter.ts, 0, 0))

a: (e: string) => void,
>a : Symbol(a, Decl(genericInferenceDefaultTypeParameter.ts, 0, 13))
>e : Symbol(e, Decl(genericInferenceDefaultTypeParameter.ts, 1, 8))

b: (e: number) => void,
>b : Symbol(b, Decl(genericInferenceDefaultTypeParameter.ts, 1, 27))
>e : Symbol(e, Decl(genericInferenceDefaultTypeParameter.ts, 2, 8))
}

function f1<T extends keyof Type = 'a'>(props: Type[T]): any {
>f1 : Symbol(f1, Decl(genericInferenceDefaultTypeParameter.ts, 3, 1))
>T : Symbol(T, Decl(genericInferenceDefaultTypeParameter.ts, 5, 12))
>Type : Symbol(Type, Decl(genericInferenceDefaultTypeParameter.ts, 0, 0))
>props : Symbol(props, Decl(genericInferenceDefaultTypeParameter.ts, 5, 40))
>Type : Symbol(Type, Decl(genericInferenceDefaultTypeParameter.ts, 0, 0))
>T : Symbol(T, Decl(genericInferenceDefaultTypeParameter.ts, 5, 12))

return null
}

f1((event) => { })
>f1 : Symbol(f1, Decl(genericInferenceDefaultTypeParameter.ts, 3, 1))
>event : Symbol(event, Decl(genericInferenceDefaultTypeParameter.ts, 9, 4))

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/genericInferenceDefaultTypeParameter.ts ===
type Type = {
>Type : { a: (e: string) => void; b: (e: number) => void; }

a: (e: string) => void,
>a : (e: string) => void
>e : string

b: (e: number) => void,
>b : (e: number) => void
>e : number
}

function f1<T extends keyof Type = 'a'>(props: Type[T]): any {
>f1 : <T extends keyof Type = "a">(props: Type[T]) => any
>props : Type[T]

return null
>null : null
}

f1((event) => { })
>f1((event) => { }) : any
>f1 : <T extends keyof Type = "a">(props: Type[T]) => any
>(event) => { } : (event: string) => void
>event : string

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [genericInferenceDefaultTypeParameterJsxReact.tsx]
/// <reference path="/.lib/react16.d.ts" />
import React, { ComponentPropsWithRef, ElementType, ReactNode } from 'react'

type ButtonBaseProps<T extends ElementType> =
ComponentPropsWithRef<T> & {
children?: ReactNode
}

function Component<T extends ElementType = 'span'>(props: ButtonBaseProps<T>) {
return <></>
}

const v1 = <Component onClick={e => e.preventDefault()} />

//// [genericInferenceDefaultTypeParameterJsxReact.js]
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
/// <reference path="react16.d.ts" />
var react_1 = __importDefault(require("react"));
function Component(props) {
return react_1["default"].createElement(react_1["default"].Fragment, null);
}
var v1 = react_1["default"].createElement(Component, { onClick: function (e) { return e.preventDefault(); } });
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/compiler/genericInferenceDefaultTypeParameterJsxReact.tsx ===
/// <reference path="react16.d.ts" />
import React, { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
>React : Symbol(React, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 6))
>ComponentPropsWithRef : Symbol(ComponentPropsWithRef, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 15))
>ElementType : Symbol(ElementType, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 38))
>ReactNode : Symbol(ReactNode, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 51))

type ButtonBaseProps<T extends ElementType> =
>ButtonBaseProps : Symbol(ButtonBaseProps, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 76))
>T : Symbol(T, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 3, 21))
>ElementType : Symbol(ElementType, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 38))

ComponentPropsWithRef<T> & {
>ComponentPropsWithRef : Symbol(ComponentPropsWithRef, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 15))
>T : Symbol(T, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 3, 21))

children?: ReactNode
>children : Symbol(children, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 4, 31))
>ReactNode : Symbol(ReactNode, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 51))
}

function Component<T extends ElementType = 'span'>(props: ButtonBaseProps<T>) {
>Component : Symbol(Component, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 6, 1))
>T : Symbol(T, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 8, 19))
>ElementType : Symbol(ElementType, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 38))
>props : Symbol(props, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 8, 51))
>ButtonBaseProps : Symbol(ButtonBaseProps, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 1, 76))
>T : Symbol(T, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 8, 19))

return <></>
}

const v1 = <Component onClick={e => e.preventDefault()} />
>v1 : Symbol(v1, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 12, 5))
>Component : Symbol(Component, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 6, 1))
>onClick : Symbol(onClick, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 12, 21))
>e : Symbol(e, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 12, 31))
>e.preventDefault : Symbol(React.SyntheticEvent.preventDefault, Decl(react16.d.ts, 642, 31))
>e : Symbol(e, Decl(genericInferenceDefaultTypeParameterJsxReact.tsx, 12, 31))
>preventDefault : Symbol(React.SyntheticEvent.preventDefault, Decl(react16.d.ts, 642, 31))

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/compiler/genericInferenceDefaultTypeParameterJsxReact.tsx ===
/// <reference path="react16.d.ts" />
import React, { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
>React : typeof React
>ComponentPropsWithRef : any
>ElementType : any
>ReactNode : any

type ButtonBaseProps<T extends ElementType> =
>ButtonBaseProps : ButtonBaseProps<T>

ComponentPropsWithRef<T> & {
children?: ReactNode
>children : React.ReactNode
}

function Component<T extends ElementType = 'span'>(props: ButtonBaseProps<T>) {
>Component : <T extends React.ElementType<any> = "span">(props: ButtonBaseProps<T>) => JSX.Element
>props : ButtonBaseProps<T>

return <></>
><></> : JSX.Element
}

const v1 = <Component onClick={e => e.preventDefault()} />
>v1 : JSX.Element
><Component onClick={e => e.preventDefault()} /> : JSX.Element
>Component : <T extends React.ElementType<any> = "span">(props: ButtonBaseProps<T>) => JSX.Element
>onClick : (e: React.MouseEvent<HTMLSpanElement>) => void
>e => e.preventDefault() : (e: React.MouseEvent<HTMLSpanElement>) => void
>e : React.MouseEvent<HTMLSpanElement>
>e.preventDefault() : void
>e.preventDefault : () => void
>e : React.MouseEvent<HTMLSpanElement>
>preventDefault : () => void

12 changes: 12 additions & 0 deletions tests/cases/compiler/genericInferenceDefaultTypeParameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @noImplicitAny: true

type Type = {
a: (e: string) => void,
b: (e: number) => void,
}

function f1<T extends keyof Type = 'a'>(props: Type[T]): any {
return null
}

f1((event) => { })
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @noImplicitAny: true
// @esModuleInterop: true
// @jsx: react

/// <reference path="/.lib/react16.d.ts" />
import React, { ComponentPropsWithRef, ElementType, ReactNode } from 'react'

type ButtonBaseProps<T extends ElementType> =
ComponentPropsWithRef<T> & {
children?: ReactNode
}

function Component<T extends ElementType = 'span'>(props: ButtonBaseProps<T>) {
return <></>
}

const v1 = <Component onClick={e => e.preventDefault()} />