Skip to content

Infer from union combined single signatures #58482

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

Open
wants to merge 1 commit 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
18 changes: 12 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25695,10 +25695,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
inferToMultipleTypes(source, (target as UnionOrIntersectionType).types, target.flags);
}
else if (source.flags & TypeFlags.Union) {
// Source is a union or intersection type, infer from each constituent type
const sourceTypes = (source as UnionOrIntersectionType).types;
for (const sourceType of sourceTypes) {
inferFromTypes(sourceType, target);
const singleSignature = getSingleCallOrConstructSignature(source);
if (singleSignature) {
inferFromTypes(getOrCreateTypeFromSignature(singleSignature), target);
}
else {
// Source is a union type, infer from each constituent type
const sourceTypes = (source as UnionType).types;
for (const sourceType of sourceTypes) {
inferFromTypes(sourceType, target);
}
}
}
else if (target.flags & TypeFlags.TemplateLiteral) {
Expand Down Expand Up @@ -33946,8 +33952,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getSingleSignature(type: Type, kind: SignatureKind, allowMembers: boolean): Signature | undefined {
if (type.flags & TypeFlags.Object) {
const resolved = resolveStructuredTypeMembers(type as ObjectType);
if (type.flags & TypeFlags.StructuredType) {
const resolved = resolveStructuredTypeMembers(type as StructuredType);
if (allowMembers || resolved.properties.length === 0 && resolved.indexInfos.length === 0) {
if (kind === SignatureKind.Call && resolved.callSignatures.length === 1 && resolved.constructSignatures.length === 0) {
return resolved.callSignatures[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/inferenceFromUnionCombinedSingleSignature1.ts] ////

=== inferenceFromUnionCombinedSingleSignature1.ts ===
// https://github.com/microsoft/TypeScript/issues/58468

declare const fn: (() => void) | ((a: number) => void);
>fn : Symbol(fn, Decl(inferenceFromUnionCombinedSingleSignature1.ts, 2, 13))
>a : Symbol(a, Decl(inferenceFromUnionCombinedSingleSignature1.ts, 2, 35))

declare const x: number;
>x : Symbol(x, Decl(inferenceFromUnionCombinedSingleSignature1.ts, 4, 13))

declare const y: any;
>y : Symbol(y, Decl(inferenceFromUnionCombinedSingleSignature1.ts, 5, 13))

fn.call(null, x);
>fn.call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>fn : Symbol(fn, Decl(inferenceFromUnionCombinedSingleSignature1.ts, 2, 13))
>call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(inferenceFromUnionCombinedSingleSignature1.ts, 4, 13))

fn.call(null, y);
>fn.call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>fn : Symbol(fn, Decl(inferenceFromUnionCombinedSingleSignature1.ts, 2, 13))
>call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>y : Symbol(y, Decl(inferenceFromUnionCombinedSingleSignature1.ts, 5, 13))

export {};

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

=== inferenceFromUnionCombinedSingleSignature1.ts ===
// https://github.com/microsoft/TypeScript/issues/58468

declare const fn: (() => void) | ((a: number) => void);
>fn : (() => void) | ((a: number) => void)
> : ^^^^^^^ ^^^^^^ ^^ ^^^^^ ^
>a : number
> : ^^^^^^

declare const x: number;
>x : number
> : ^^^^^^

declare const y: any;
>y : any

fn.call(null, x);
>fn.call(null, x) : void
> : ^^^^
>fn.call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
> : ^ ^^ ^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ ^^ ^^^^^^
>fn : (() => void) | ((a: number) => void)
> : ^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^
>call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
> : ^ ^^ ^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ ^^ ^^^^^^
>x : number
> : ^^^^^^

fn.call(null, y);
>fn.call(null, y) : void
> : ^^^^
>fn.call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
> : ^ ^^ ^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ ^^ ^^^^^^
>fn : (() => void) | ((a: number) => void)
> : ^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^
>call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
> : ^ ^^ ^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ ^^ ^^^^^^
>y : any

export {};

2 changes: 1 addition & 1 deletion tests/baselines/reference/jsDeclarationsGetterSetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,5 @@ export class L {
set x(value: any);
}
export class M {
set x(value: any);
set x(value: boolean);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comes from:

/**
 * @param {number | boolean} v
 */
const mSetter1 = (v) => {}
/**
 * @param {string | boolean} v
 */
const mSetter2 = (v) => {}
export class M {}
Object.defineProperty(M.prototype, "x", {
    set: Math.random() ? mSetter1 : mSetter2
});

This feels like an improvement, those setter functions can be combined safely to a single signature with a boolean parameter

}
4 changes: 2 additions & 2 deletions tests/baselines/reference/neverIntersectionNotCallable.types
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ declare const f: { (x: string): number, a: "" } & { a: number }
> : ^^^^^^

f()
>f() : any
> : ^^^
>f() : number
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function itself is still not callable here but that erroneous call shows a better return type now. This is caused by getSingleSignature handling structured types now (an intersection in this very case)

> : ^^^^^^
>f : never
> : ^^^^^

14 changes: 14 additions & 0 deletions tests/cases/compiler/inferenceFromUnionCombinedSingleSignature1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/58468

declare const fn: (() => void) | ((a: number) => void);

declare const x: number;
declare const y: any;

fn.call(null, x);
fn.call(null, y);

export {};