Add specific weak type error for callable types

"Did you mean to call it?"
This commit is contained in:
Nathan Shively-Sanders 2017-08-08 11:25:32 -07:00
parent 781da2332d
commit 7ff1d8e797
5 changed files with 27 additions and 15 deletions

View File

@ -8944,7 +8944,15 @@ namespace ts {
isWeakType(target) &&
!hasCommonProperties(source, target)) {
if (reportErrors) {
reportError(Diagnostics.Type_0_has_no_properties_in_common_with_type_1, typeToString(source), typeToString(target));
const calls = getSignaturesOfType(source, SignatureKind.Call);
const constructs = getSignaturesOfType(source, SignatureKind.Construct);
if (calls.length > 0 && isRelatedTo(getReturnTypeOfSignature(calls[0]), target, /*reportErrors*/ false) ||
constructs.length > 0 && isRelatedTo(getReturnTypeOfSignature(constructs[0]), target, /*reportErrors*/ false)) {
reportError(Diagnostics.Value_of_type_0_has_no_properties_in_common_with_type_1_Did_you_mean_to_call_it, typeToString(source), typeToString(target));
}
else {
reportError(Diagnostics.Type_0_has_no_properties_in_common_with_type_1, typeToString(source), typeToString(target));
}
}
return Ternary.False;
}

View File

@ -1908,6 +1908,10 @@
"category": "Error",
"code": 2559
},
"Value of type '{0}' has no properties in common with type '{1}'. Did you mean to call it?": {
"category": "Error",
"code": 2560
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600

View File

@ -1,6 +1,6 @@
tests/cases/compiler/weakType.ts(15,13): error TS2559: Type '() => { timeout: number; }' has no properties in common with type 'Settings'.
tests/cases/compiler/weakType.ts(16,13): error TS2559: Type '() => void' has no properties in common with type 'Settings'.
tests/cases/compiler/weakType.ts(17,13): error TS2559: Type 'CtorOnly' has no properties in common with type 'Settings'.
tests/cases/compiler/weakType.ts(15,13): error TS2560: Value of type '() => { timeout: number; }' has no properties in common with type 'Settings'. Did you mean to call it?
tests/cases/compiler/weakType.ts(16,13): error TS2560: Value of type '() => { timeout: number; }' has no properties in common with type 'Settings'. Did you mean to call it?
tests/cases/compiler/weakType.ts(17,13): error TS2560: Value of type 'CtorOnly' has no properties in common with type 'Settings'. Did you mean to call it?
tests/cases/compiler/weakType.ts(18,13): error TS2559: Type '12' has no properties in common with type 'Settings'.
tests/cases/compiler/weakType.ts(19,13): error TS2559: Type '"completely wrong"' has no properties in common with type 'Settings'.
tests/cases/compiler/weakType.ts(20,13): error TS2559: Type 'false' has no properties in common with type 'Settings'.
@ -21,20 +21,20 @@ tests/cases/compiler/weakType.ts(62,5): error TS2322: Type '{ properties: { wron
return { timeout: 1000 };
}
interface CtorOnly {
new(s: string): string
new(s: string): { timeout: 1000 }
}
function doSomething(settings: Settings) { /* ... */ }
// forgot to call `getDefaultSettings`
doSomething(getDefaultSettings);
~~~~~~~~~~~~~~~~~~
!!! error TS2559: Type '() => { timeout: number; }' has no properties in common with type 'Settings'.
doSomething(() => { });
~~~~~~~~~
!!! error TS2559: Type '() => void' has no properties in common with type 'Settings'.
!!! error TS2560: Value of type '() => { timeout: number; }' has no properties in common with type 'Settings'. Did you mean to call it?
doSomething(() => ({ timeout: 1000 }));
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2560: Value of type '() => { timeout: number; }' has no properties in common with type 'Settings'. Did you mean to call it?
doSomething(null as CtorOnly);
~~~~~~~~~~~~~~~~
!!! error TS2559: Type 'CtorOnly' has no properties in common with type 'Settings'.
!!! error TS2560: Value of type 'CtorOnly' has no properties in common with type 'Settings'. Did you mean to call it?
doSomething(12);
~~
!!! error TS2559: Type '12' has no properties in common with type 'Settings'.

View File

@ -8,13 +8,13 @@ function getDefaultSettings() {
return { timeout: 1000 };
}
interface CtorOnly {
new(s: string): string
new(s: string): { timeout: 1000 }
}
function doSomething(settings: Settings) { /* ... */ }
// forgot to call `getDefaultSettings`
doSomething(getDefaultSettings);
doSomething(() => { });
doSomething(() => ({ timeout: 1000 }));
doSomething(null as CtorOnly);
doSomething(12);
doSomething('completely wrong');
@ -71,7 +71,7 @@ function getDefaultSettings() {
function doSomething(settings) { }
// forgot to call `getDefaultSettings`
doSomething(getDefaultSettings);
doSomething(function () { });
doSomething(function () { return ({ timeout: 1000 }); });
doSomething(null);
doSomething(12);
doSomething('completely wrong');

View File

@ -7,13 +7,13 @@ function getDefaultSettings() {
return { timeout: 1000 };
}
interface CtorOnly {
new(s: string): string
new(s: string): { timeout: 1000 }
}
function doSomething(settings: Settings) { /* ... */ }
// forgot to call `getDefaultSettings`
doSomething(getDefaultSettings);
doSomething(() => { });
doSomething(() => ({ timeout: 1000 }));
doSomething(null as CtorOnly);
doSomething(12);
doSomething('completely wrong');