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

Infer type parameters only from applicable contextual types #52622

Closed
wants to merge 2 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
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31929,10 +31929,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// return type of 'wrap'.
if (node.kind !== SyntaxKind.Decorator) {
const skipBindingPatterns = every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p));
const contextualType = getContextualType(node, skipBindingPatterns ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
let contextualType = getContextualType(node, skipBindingPatterns ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
if (contextualType) {
const inferenceTargetType = getReturnTypeOfSignature(signature);
if (couldContainTypeVariables(inferenceTargetType)) {
const targetConstraint = getConstraintOfType(inferenceTargetType);
contextualType = targetConstraint ? getIntersectionType([contextualType, targetConstraint]) : contextualType;
const outerContext = getInferenceContext(node);
const isFromBindingPattern = !skipBindingPatterns && getContextualType(node, ContextFlags.SkipBindingPatterns) !== contextualType;
// A return type inference from a binding pattern can be used in instantiating the contextual
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,8 @@ export let sys: System = (() => {
disableCPUProfiler,
cpuProfilingEnabled: () => !!activeSession || contains(process.execArgv, "--cpu-prof") || contains(process.execArgv, "--prof"),
realpath,
debugMode: !!process.env.NODE_INSPECTOR_IPC || !!process.env.VSCODE_INSPECTOR_OPTIONS || some(process.execArgv as string[], arg => /^--(inspect|debug)(-brk)?(=\d+)?$/i.test(arg)),
// debugMode: !!process.env.NODE_INSPECTOR_IPC || !!process.env.VSCODE_INSPECTOR_OPTIONS || some(process.execArgv as string[], arg => /^--(inspect|debug)(-brk)?(=\d+)?$/i.test(arg)),
debugMode: true,
tryEnableSourceMapsForHost() {
try {
(require("source-map-support") as typeof import("source-map-support")).install();
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/deepComparisons.types
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function g() {

return f() as F<any>;
>f() as F<any> : F<any>
>f() : F<any>
>f() : F<T>
>f : <T = any>() => F<T>
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/compiler/inferTypeParameterFromApplicableContextualType.ts ===
declare function useCallback<T extends Function>(fn: T): T;
>useCallback : Symbol(useCallback, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 0))
>T : Symbol(T, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 29))
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>fn : Symbol(fn, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 49))
>T : Symbol(T, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 29))
>T : Symbol(T, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 29))

declare function ex1(callback: (x: number) => void): void;
>ex1 : Symbol(ex1, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 59))
>callback : Symbol(callback, Decl(inferTypeParameterFromApplicableContextualType.ts, 2, 21))
>x : Symbol(x, Decl(inferTypeParameterFromApplicableContextualType.ts, 2, 32))

ex1(useCallback(x => {}));
>ex1 : Symbol(ex1, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 59))
>useCallback : Symbol(useCallback, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 0))
>x : Symbol(x, Decl(inferTypeParameterFromApplicableContextualType.ts, 3, 16))

declare function ex2(callback?: (x: number) => void): void;
>ex2 : Symbol(ex2, Decl(inferTypeParameterFromApplicableContextualType.ts, 3, 26))
>callback : Symbol(callback, Decl(inferTypeParameterFromApplicableContextualType.ts, 5, 21))
>x : Symbol(x, Decl(inferTypeParameterFromApplicableContextualType.ts, 5, 33))

ex2(useCallback(x => {}));
>ex2 : Symbol(ex2, Decl(inferTypeParameterFromApplicableContextualType.ts, 3, 26))
>useCallback : Symbol(useCallback, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 0))
>x : Symbol(x, Decl(inferTypeParameterFromApplicableContextualType.ts, 6, 16))

declare function ex3(callback: ((x: number) => void) | 5): void;
>ex3 : Symbol(ex3, Decl(inferTypeParameterFromApplicableContextualType.ts, 6, 26))
>callback : Symbol(callback, Decl(inferTypeParameterFromApplicableContextualType.ts, 8, 21))
>x : Symbol(x, Decl(inferTypeParameterFromApplicableContextualType.ts, 8, 33))

ex3(useCallback(x => {}));
>ex3 : Symbol(ex3, Decl(inferTypeParameterFromApplicableContextualType.ts, 6, 26))
>useCallback : Symbol(useCallback, Decl(inferTypeParameterFromApplicableContextualType.ts, 0, 0))
>x : Symbol(x, Decl(inferTypeParameterFromApplicableContextualType.ts, 9, 16))

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
=== tests/cases/compiler/inferTypeParameterFromApplicableContextualType.ts ===
declare function useCallback<T extends Function>(fn: T): T;
>useCallback : <T extends Function>(fn: T) => T
>fn : T

declare function ex1(callback: (x: number) => void): void;
>ex1 : (callback: (x: number) => void) => void
>callback : (x: number) => void
>x : number

ex1(useCallback(x => {}));
>ex1(useCallback(x => {})) : void
>ex1 : (callback: (x: number) => void) => void
>useCallback(x => {}) : (x: number) => void
>useCallback : <T extends Function>(fn: T) => T
>x => {} : (x: number) => void
>x : number

declare function ex2(callback?: (x: number) => void): void;
>ex2 : (callback?: ((x: number) => void) | undefined) => void
>callback : ((x: number) => void) | undefined
>x : number

ex2(useCallback(x => {}));
>ex2(useCallback(x => {})) : void
>ex2 : (callback?: ((x: number) => void) | undefined) => void
>useCallback(x => {}) : (x: number) => void
>useCallback : <T extends Function>(fn: T) => T
>x => {} : (x: number) => void
>x : number

declare function ex3(callback: ((x: number) => void) | 5): void;
>ex3 : (callback: ((x: number) => void) | 5) => void
>callback : ((x: number) => void) | 5
>x : number

ex3(useCallback(x => {}));
>ex3(useCallback(x => {})) : void
>ex3 : (callback: ((x: number) => void) | 5) => void
>useCallback(x => {}) : (x: number) => void
>useCallback : <T extends Function>(fn: T) => T
>x => {} : (x: number) => void
>x : number

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
=== tests/cases/compiler/nonnullAssertionPropegatesContextualType.ts ===
let rect2: SVGRectElement = document.querySelector('.svg-rectangle')!; // Error: Element
>rect2 : SVGRectElement
>document.querySelector('.svg-rectangle')! : SVGRectElement
>document.querySelector('.svg-rectangle') : SVGRectElement | null
>document.querySelector('.svg-rectangle')! : SVGRectElement & Element
>document.querySelector('.svg-rectangle') : (SVGRectElement & Element) | null
>document.querySelector : { <K extends keyof HTMLElementTagNameMap>(selectors: K): HTMLElementTagNameMap[K] | null; <K extends keyof SVGElementTagNameMap>(selectors: K): SVGElementTagNameMap[K] | null; <K extends keyof MathMLElementTagNameMap>(selectors: K): MathMLElementTagNameMap[K] | null; <K extends keyof HTMLElementDeprecatedTagNameMap>(selectors: K): HTMLElementDeprecatedTagNameMap[K] | null; <E extends Element = Element>(selectors: string): E | null; }
>document : Document
>querySelector : { <K extends keyof HTMLElementTagNameMap>(selectors: K): HTMLElementTagNameMap[K] | null; <K extends keyof SVGElementTagNameMap>(selectors: K): SVGElementTagNameMap[K] | null; <K extends keyof MathMLElementTagNameMap>(selectors: K): MathMLElementTagNameMap[K] | null; <K extends keyof HTMLElementDeprecatedTagNameMap>(selectors: K): HTMLElementDeprecatedTagNameMap[K] | null; <E extends Element = Element>(selectors: string): E | null; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @strict: true
// @noEmit: true

declare function useCallback<T extends Function>(fn: T): T;

declare function ex1(callback: (x: number) => void): void;
ex1(useCallback(x => {}));

declare function ex2(callback?: (x: number) => void): void;
ex2(useCallback(x => {}));

declare function ex3(callback: ((x: number) => void) | 5): void;
ex3(useCallback(x => {}));