Weak types must not have call or construct sigs

This changes the definition of weak types
This commit is contained in:
Nathan Shively-Sanders 2017-05-26 10:19:20 -07:00
parent d1d487cf40
commit 548f92ad34
4 changed files with 39 additions and 1 deletions

View file

@ -9389,13 +9389,15 @@ namespace ts {
/**
* A type is 'weak' if it is an object type with at least one optional property
* and no required properties or index signatures
* and no required properties, call/construct signatures or index signatures
*/
function isWeak(type: Type) {
const props = getPropertiesOfType(type);
return type.flags & TypeFlags.Object &&
props.length > 0 &&
every(props, p => !!(p.flags & SymbolFlags.Optional)) &&
!getSignaturesOfType(type, SignatureKind.Call).length &&
!getSignaturesOfType(type, SignatureKind.Construct).length &&
!getIndexTypeOfType(type, IndexKind.String) &&
!getIndexTypeOfType(type, IndexKind.Number);
}

View file

@ -38,4 +38,14 @@ tests/cases/compiler/weakType.ts(31,18): error TS2345: Argument of type '{ error
!!! error TS2345: Argument of type '{ error?: number; }' is not assignable to parameter of type 'ChangeOptions'.
!!! error TS2345: Weak type 'ChangeOptions' has no properties in common with '{ error?: number; }'.
}
class K {
constructor(s: string) { }
}
// Ctor isn't a weak type because it has a construct signature
interface Ctor {
new (s: string): K
n?: number
}
let ctor: Ctor = K

View file

@ -31,6 +31,16 @@ function del(options: ConfigurableStartEnd = {},
changes.push(options);
changes.push(error);
}
class K {
constructor(s: string) { }
}
// Ctor isn't a weak type because it has a construct signature
interface Ctor {
new (s: string): K
n?: number
}
let ctor: Ctor = K
//// [weakType.js]
@ -48,3 +58,9 @@ function del(options, error) {
changes.push(options);
changes.push(error);
}
var K = (function () {
function K(s) {
}
return K;
}());
var ctor = K;

View file

@ -30,3 +30,13 @@ function del(options: ConfigurableStartEnd = {},
changes.push(options);
changes.push(error);
}
class K {
constructor(s: string) { }
}
// Ctor isn't a weak type because it has a construct signature
interface Ctor {
new (s: string): K
n?: number
}
let ctor: Ctor = K