Skip to content

Find first callable/constructable type in union when appropriate #26895

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

Merged
merged 6 commits into from
Sep 7, 2018
Merged
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
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11395,7 +11395,8 @@ namespace ts {
const bestMatchingType =
findMatchingDiscriminantType(source, target) ||
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
findBestTypeForObjectLiteral(source, target);
findBestTypeForObjectLiteral(source, target) ||
findBestTypeForInvokable(source, target);

isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
}
Expand Down Expand Up @@ -11426,6 +11427,15 @@ namespace ts {
}
}

function findBestTypeForInvokable(source: Type, unionTarget: UnionOrIntersectionType) {
let signatureKind = SignatureKind.Call;
const hasSignatures = getSignaturesOfType(source, signatureKind).length > 0 ||
(signatureKind = SignatureKind.Construct, getSignaturesOfType(source, signatureKind).length > 0);
if (hasSignatures) {
return find(unionTarget.types, t => getSignaturesOfType(t, signatureKind).length > 0);
}
}

// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {
let match: Type | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(58,5): error TS2322: Type '(a: string, b: number) => string | number' is not assignable to type '((a: string, b: number) => string) | ((a: string, b: number) => number)'.
Type '(a: string, b: number) => string | number' is not assignable to type '(a: string, b: number) => number'.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
Type '(a: string, b: number) => string | number' is not assignable to type '(a: string, b: number) => string'.
Copy link
Member

Choose a reason for hiding this comment

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

We could avoid these baseline changes if we choose the last matching invokable instead of the first.

Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.


==== tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts (6 errors) ====
Expand Down Expand Up @@ -116,8 +116,8 @@ tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(
commonMethodDifferentReturnType: (a, b) => strOrNumber,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '(a: string, b: number) => string | number' is not assignable to type '((a: string, b: number) => string) | ((a: string, b: number) => number)'.
!!! error TS2322: Type '(a: string, b: number) => string | number' is not assignable to type '(a: string, b: number) => number'.
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
!!! error TS2322: Type '(a: string, b: number) => string | number' is not assignable to type '(a: string, b: number) => string'.
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
!!! related TS6500 tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts:35:5: The expected type comes from property 'commonMethodDifferentReturnType' which is declared here on type 'I11 | I21'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
tests/cases/compiler/errorsWithInvokablesInUnions01.ts(14,12): error TS2322: Type '(x: string) => void' is not assignable to type 'ConstructableA | IDirectiveLinkFn<number> | IDirectivePrePost<number>'.
Type '(x: string) => void' is not assignable to type 'IDirectiveLinkFn<number>'.
Types of parameters 'x' and 'scope' are incompatible.
Type 'number' is not assignable to type 'string'.
tests/cases/compiler/errorsWithInvokablesInUnions01.ts(16,12): error TS2322: Type 'typeof ctor' is not assignable to type 'ConstructableA | IDirectiveLinkFn<number> | IDirectivePrePost<number>'.
Type 'typeof ctor' is not assignable to type 'ConstructableA'.
Type 'ctor' is not assignable to type '{ somePropA: any; }'.
Property 'somePropA' is missing in type 'ctor'.


==== tests/cases/compiler/errorsWithInvokablesInUnions01.ts (2 errors) ====
interface ConstructableA {
new(): { somePropA: any };
}

interface IDirectiveLinkFn<TScope> {
(scope: TScope): void;
}

interface IDirectivePrePost<TScope> {
pre?: IDirectiveLinkFn<TScope>;
post?: IDirectiveLinkFn<TScope>;
}

export let blah: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = (x: string) => {}
~~~~
!!! error TS2322: Type '(x: string) => void' is not assignable to type 'ConstructableA | IDirectiveLinkFn<number> | IDirectivePrePost<number>'.
!!! error TS2322: Type '(x: string) => void' is not assignable to type 'IDirectiveLinkFn<number>'.
!!! error TS2322: Types of parameters 'x' and 'scope' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'string'.

export let ctor: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = class {
~~~~
!!! error TS2322: Type 'typeof ctor' is not assignable to type 'ConstructableA | IDirectiveLinkFn<number> | IDirectivePrePost<number>'.
!!! error TS2322: Type 'typeof ctor' is not assignable to type 'ConstructableA'.
!!! error TS2322: Type 'ctor' is not assignable to type '{ somePropA: any; }'.
!!! error TS2322: Property 'somePropA' is missing in type 'ctor'.
someUnaccountedProp: any;
}

30 changes: 30 additions & 0 deletions tests/baselines/reference/errorsWithInvokablesInUnions01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [errorsWithInvokablesInUnions01.ts]
interface ConstructableA {
new(): { somePropA: any };
}

interface IDirectiveLinkFn<TScope> {
(scope: TScope): void;
}

interface IDirectivePrePost<TScope> {
pre?: IDirectiveLinkFn<TScope>;
post?: IDirectiveLinkFn<TScope>;
}

export let blah: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = (x: string) => {}

export let ctor: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = class {
someUnaccountedProp: any;
}


//// [errorsWithInvokablesInUnions01.js]
"use strict";
exports.__esModule = true;
exports.blah = function (x) { };
exports.ctor = /** @class */ (function () {
function class_1() {
}
return class_1;
}());
49 changes: 49 additions & 0 deletions tests/baselines/reference/errorsWithInvokablesInUnions01.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== tests/cases/compiler/errorsWithInvokablesInUnions01.ts ===
interface ConstructableA {
>ConstructableA : Symbol(ConstructableA, Decl(errorsWithInvokablesInUnions01.ts, 0, 0))

new(): { somePropA: any };
>somePropA : Symbol(somePropA, Decl(errorsWithInvokablesInUnions01.ts, 1, 10))
}

interface IDirectiveLinkFn<TScope> {
>IDirectiveLinkFn : Symbol(IDirectiveLinkFn, Decl(errorsWithInvokablesInUnions01.ts, 2, 1))
>TScope : Symbol(TScope, Decl(errorsWithInvokablesInUnions01.ts, 4, 27))

(scope: TScope): void;
>scope : Symbol(scope, Decl(errorsWithInvokablesInUnions01.ts, 5, 5))
>TScope : Symbol(TScope, Decl(errorsWithInvokablesInUnions01.ts, 4, 27))
}

interface IDirectivePrePost<TScope> {
>IDirectivePrePost : Symbol(IDirectivePrePost, Decl(errorsWithInvokablesInUnions01.ts, 6, 1))
>TScope : Symbol(TScope, Decl(errorsWithInvokablesInUnions01.ts, 8, 28))

pre?: IDirectiveLinkFn<TScope>;
>pre : Symbol(IDirectivePrePost.pre, Decl(errorsWithInvokablesInUnions01.ts, 8, 37))
>IDirectiveLinkFn : Symbol(IDirectiveLinkFn, Decl(errorsWithInvokablesInUnions01.ts, 2, 1))
>TScope : Symbol(TScope, Decl(errorsWithInvokablesInUnions01.ts, 8, 28))

post?: IDirectiveLinkFn<TScope>;
>post : Symbol(IDirectivePrePost.post, Decl(errorsWithInvokablesInUnions01.ts, 9, 35))
>IDirectiveLinkFn : Symbol(IDirectiveLinkFn, Decl(errorsWithInvokablesInUnions01.ts, 2, 1))
>TScope : Symbol(TScope, Decl(errorsWithInvokablesInUnions01.ts, 8, 28))
}

export let blah: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = (x: string) => {}
>blah : Symbol(blah, Decl(errorsWithInvokablesInUnions01.ts, 13, 10))
>IDirectiveLinkFn : Symbol(IDirectiveLinkFn, Decl(errorsWithInvokablesInUnions01.ts, 2, 1))
>ConstructableA : Symbol(ConstructableA, Decl(errorsWithInvokablesInUnions01.ts, 0, 0))
>IDirectivePrePost : Symbol(IDirectivePrePost, Decl(errorsWithInvokablesInUnions01.ts, 6, 1))
>x : Symbol(x, Decl(errorsWithInvokablesInUnions01.ts, 13, 90))

export let ctor: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = class {
>ctor : Symbol(ctor, Decl(errorsWithInvokablesInUnions01.ts, 15, 10))
>IDirectiveLinkFn : Symbol(IDirectiveLinkFn, Decl(errorsWithInvokablesInUnions01.ts, 2, 1))
>ConstructableA : Symbol(ConstructableA, Decl(errorsWithInvokablesInUnions01.ts, 0, 0))
>IDirectivePrePost : Symbol(IDirectivePrePost, Decl(errorsWithInvokablesInUnions01.ts, 6, 1))

someUnaccountedProp: any;
>someUnaccountedProp : Symbol(ctor.someUnaccountedProp, Decl(errorsWithInvokablesInUnions01.ts, 15, 96))
}

32 changes: 32 additions & 0 deletions tests/baselines/reference/errorsWithInvokablesInUnions01.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/compiler/errorsWithInvokablesInUnions01.ts ===
interface ConstructableA {
new(): { somePropA: any };
>somePropA : any
}

interface IDirectiveLinkFn<TScope> {
(scope: TScope): void;
>scope : TScope
}

interface IDirectivePrePost<TScope> {
pre?: IDirectiveLinkFn<TScope>;
>pre : IDirectiveLinkFn<TScope>

post?: IDirectiveLinkFn<TScope>;
>post : IDirectiveLinkFn<TScope>
}

export let blah: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = (x: string) => {}
>blah : ConstructableA | IDirectiveLinkFn<number> | IDirectivePrePost<number>
>(x: string) => {} : (x: string) => void
>x : string

export let ctor: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = class {
>ctor : ConstructableA | IDirectiveLinkFn<number> | IDirectivePrePost<number>
>class { someUnaccountedProp: any;} : typeof ctor

someUnaccountedProp: any;
>someUnaccountedProp : any
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/expressions/contextualTyping/functionExpressionContextualTyping2.ts(11,1): error TS2322: Type '(foo: number, bar: string) => boolean' is not assignable to type '((n: number, s: string) => number) | ((n: number, s: string) => string)'.
Type '(foo: number, bar: string) => boolean' is not assignable to type '(n: number, s: string) => string'.
Type 'boolean' is not assignable to type 'string'.
Type '(foo: number, bar: string) => boolean' is not assignable to type '(n: number, s: string) => number'.
Type 'boolean' is not assignable to type 'number'.


==== tests/cases/conformance/expressions/contextualTyping/functionExpressionContextualTyping2.ts (1 errors) ====
Expand All @@ -17,5 +17,5 @@ tests/cases/conformance/expressions/contextualTyping/functionExpressionContextua
a1 = (foo, bar) => { return true; } // Error
~~
!!! error TS2322: Type '(foo: number, bar: string) => boolean' is not assignable to type '((n: number, s: string) => number) | ((n: number, s: string) => string)'.
!!! error TS2322: Type '(foo: number, bar: string) => boolean' is not assignable to type '(n: number, s: string) => string'.
!!! error TS2322: Type 'boolean' is not assignable to type 'string'.
!!! error TS2322: Type '(foo: number, bar: string) => boolean' is not assignable to type '(n: number, s: string) => number'.
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
18 changes: 18 additions & 0 deletions tests/cases/compiler/errorsWithInvokablesInUnions01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interface ConstructableA {
new(): { somePropA: any };
}

interface IDirectiveLinkFn<TScope> {
(scope: TScope): void;
}

interface IDirectivePrePost<TScope> {
pre?: IDirectiveLinkFn<TScope>;
post?: IDirectiveLinkFn<TScope>;
}

export let blah: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = (x: string) => {}

export let ctor: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<number> = class {
someUnaccountedProp: any;
}