diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4dd2058d4d3..2b910e6abb8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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; } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 3b4b0ddf667..03164c54ffd 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 diff --git a/tests/baselines/reference/weakType.errors.txt b/tests/baselines/reference/weakType.errors.txt index b08dcc33980..ffc1d237593 100644 --- a/tests/baselines/reference/weakType.errors.txt +++ b/tests/baselines/reference/weakType.errors.txt @@ -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'. diff --git a/tests/baselines/reference/weakType.js b/tests/baselines/reference/weakType.js index 999269384dc..2a1dc4ca0e4 100644 --- a/tests/baselines/reference/weakType.js +++ b/tests/baselines/reference/weakType.js @@ -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'); diff --git a/tests/cases/compiler/weakType.ts b/tests/cases/compiler/weakType.ts index 8fda5df9166..08c9d95e672 100644 --- a/tests/cases/compiler/weakType.ts +++ b/tests/cases/compiler/weakType.ts @@ -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');