Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -8938,7 +8938,9 @@ namespace ts {
!(target.flags & TypeFlags.Union) &&
!isIntersectionConstituent &&
source !== globalObjectType &&
getPropertiesOfType(source).length > 0 &&
(getPropertiesOfType(source).length > 0 ||
getSignaturesOfType(source, SignatureKind.Call).length > 0 ||
getSignaturesOfType(source, SignatureKind.Construct).length > 0) &&
isWeakType(target) &&
!hasCommonProperties(source, target)) {
if (reportErrors) {
Expand Down
28 changes: 20 additions & 8 deletions tests/baselines/reference/weakType.errors.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
tests/cases/compiler/weakType.ts(16,13): error TS2559: Type '12' has no properties in common with type 'Settings'.
tests/cases/compiler/weakType.ts(17,13): error TS2559: Type '"completely wrong"' has no properties in common with type 'Settings'.
tests/cases/compiler/weakType.ts(18,13): error TS2559: Type 'false' has no properties in common with type 'Settings'.
tests/cases/compiler/weakType.ts(35,18): error TS2559: Type '{ error?: number; }' has no properties in common with type 'ChangeOptions'.
tests/cases/compiler/weakType.ts(60,5): error TS2322: Type '{ properties: { wrong: string; }; }' is not assignable to type 'Weak & Spoiler'.
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(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'.
tests/cases/compiler/weakType.ts(37,18): error TS2559: Type '{ error?: number; }' has no properties in common with type 'ChangeOptions'.
tests/cases/compiler/weakType.ts(62,5): error TS2322: Type '{ properties: { wrong: string; }; }' is not assignable to type 'Weak & Spoiler'.
Type '{ properties: { wrong: string; }; }' is not assignable to type 'Weak'.
Types of property 'properties' are incompatible.
Type '{ wrong: string; }' has no properties in common with type '{ b?: number; }'.


==== tests/cases/compiler/weakType.ts (5 errors) ====
==== tests/cases/compiler/weakType.ts (8 errors) ====
interface Settings {
timeout?: number;
onError?(): void;
Expand All @@ -17,13 +20,21 @@ tests/cases/compiler/weakType.ts(60,5): error TS2322: Type '{ properties: { wron
function getDefaultSettings() {
return { timeout: 1000 };
}
interface CtorOnly {
new(s: string): string
}

function doSomething(settings: Settings) { /* ... */ }
// forgot to call `getDefaultSettings`
// but it is not caught because we don't check for call signatures
doSomething(getDefaultSettings);
// same for arrow expressions:
~~~~~~~~~~~~~~~~~~
!!! 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'.
doSomething(null as CtorOnly);
~~~~~~~~~~~~~~~~
!!! error TS2559: Type 'CtorOnly' has no properties in common with type 'Settings'.
doSomething(12);
~~
!!! error TS2559: Type '12' has no properties in common with type 'Settings'.
Expand Down Expand Up @@ -82,4 +93,5 @@ tests/cases/compiler/weakType.ts(60,5): error TS2322: Type '{ properties: { wron
!!! error TS2322: Type '{ properties: { wrong: string; }; }' is not assignable to type 'Weak'.
!!! error TS2322: Types of property 'properties' are incompatible.
!!! error TS2322: Type '{ wrong: string; }' has no properties in common with type '{ b?: number; }'.


10 changes: 6 additions & 4 deletions tests/baselines/reference/weakType.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ interface Settings {
function getDefaultSettings() {
return { timeout: 1000 };
}
interface CtorOnly {
new(s: string): string
}

function doSomething(settings: Settings) { /* ... */ }
// forgot to call `getDefaultSettings`
// but it is not caught because we don't check for call signatures
doSomething(getDefaultSettings);
// same for arrow expressions:
doSomething(() => { });
doSomething(null as CtorOnly);
doSomething(12);
doSomething('completely wrong');
doSomething(false);
Expand Down Expand Up @@ -59,6 +61,7 @@ declare let unknown: {
}
}
let weak: Weak & Spoiler = unknown



//// [weakType.js]
Expand All @@ -67,10 +70,9 @@ function getDefaultSettings() {
}
function doSomething(settings) { }
// forgot to call `getDefaultSettings`
// but it is not caught because we don't check for call signatures
doSomething(getDefaultSettings);
// same for arrow expressions:
doSomething(function () { });
doSomething(null);
doSomething(12);
doSomething('completely wrong');
doSomething(false);
Expand Down
7 changes: 5 additions & 2 deletions tests/cases/compiler/weakType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ interface Settings {
function getDefaultSettings() {
return { timeout: 1000 };
}
interface CtorOnly {
new(s: string): string
}

function doSomething(settings: Settings) { /* ... */ }
// forgot to call `getDefaultSettings`
// but it is not caught because we don't check for call signatures
doSomething(getDefaultSettings);
// same for arrow expressions:
doSomething(() => { });
doSomething(null as CtorOnly);
doSomething(12);
doSomething('completely wrong');
doSomething(false);
Expand Down Expand Up @@ -58,3 +60,4 @@ declare let unknown: {
}
}
let weak: Weak & Spoiler = unknown