Skip to content

Commit 548f92a

Browse files
committed
Weak types must not have call or construct sigs
This changes the definition of weak types
1 parent d1d487c commit 548f92a

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9389,13 +9389,15 @@ namespace ts {
93899389

93909390
/**
93919391
* A type is 'weak' if it is an object type with at least one optional property
9392-
* and no required properties or index signatures
9392+
* and no required properties, call/construct signatures or index signatures
93939393
*/
93949394
function isWeak(type: Type) {
93959395
const props = getPropertiesOfType(type);
93969396
return type.flags & TypeFlags.Object &&
93979397
props.length > 0 &&
93989398
every(props, p => !!(p.flags & SymbolFlags.Optional)) &&
9399+
!getSignaturesOfType(type, SignatureKind.Call).length &&
9400+
!getSignaturesOfType(type, SignatureKind.Construct).length &&
93999401
!getIndexTypeOfType(type, IndexKind.String) &&
94009402
!getIndexTypeOfType(type, IndexKind.Number);
94019403
}

tests/baselines/reference/weakType.errors.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ tests/cases/compiler/weakType.ts(31,18): error TS2345: Argument of type '{ error
3838
!!! error TS2345: Argument of type '{ error?: number; }' is not assignable to parameter of type 'ChangeOptions'.
3939
!!! error TS2345: Weak type 'ChangeOptions' has no properties in common with '{ error?: number; }'.
4040
}
41+
42+
class K {
43+
constructor(s: string) { }
44+
}
45+
// Ctor isn't a weak type because it has a construct signature
46+
interface Ctor {
47+
new (s: string): K
48+
n?: number
49+
}
50+
let ctor: Ctor = K
4151

tests/baselines/reference/weakType.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ function del(options: ConfigurableStartEnd = {},
3131
changes.push(options);
3232
changes.push(error);
3333
}
34+
35+
class K {
36+
constructor(s: string) { }
37+
}
38+
// Ctor isn't a weak type because it has a construct signature
39+
interface Ctor {
40+
new (s: string): K
41+
n?: number
42+
}
43+
let ctor: Ctor = K
3444

3545

3646
//// [weakType.js]
@@ -48,3 +58,9 @@ function del(options, error) {
4858
changes.push(options);
4959
changes.push(error);
5060
}
61+
var K = (function () {
62+
function K(s) {
63+
}
64+
return K;
65+
}());
66+
var ctor = K;

tests/cases/compiler/weakType.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ function del(options: ConfigurableStartEnd = {},
3030
changes.push(options);
3131
changes.push(error);
3232
}
33+
34+
class K {
35+
constructor(s: string) { }
36+
}
37+
// Ctor isn't a weak type because it has a construct signature
38+
interface Ctor {
39+
new (s: string): K
40+
n?: number
41+
}
42+
let ctor: Ctor = K

0 commit comments

Comments
 (0)