Weak types must not have call or construct sigs
This changes the definition of weak types
This commit is contained in:
parent
d1d487cf40
commit
548f92ad34
|
@ -9389,13 +9389,15 @@ namespace ts {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A type is 'weak' if it is an object type with at least one optional property
|
* 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) {
|
function isWeak(type: Type) {
|
||||||
const props = getPropertiesOfType(type);
|
const props = getPropertiesOfType(type);
|
||||||
return type.flags & TypeFlags.Object &&
|
return type.flags & TypeFlags.Object &&
|
||||||
props.length > 0 &&
|
props.length > 0 &&
|
||||||
every(props, p => !!(p.flags & SymbolFlags.Optional)) &&
|
every(props, p => !!(p.flags & SymbolFlags.Optional)) &&
|
||||||
|
!getSignaturesOfType(type, SignatureKind.Call).length &&
|
||||||
|
!getSignaturesOfType(type, SignatureKind.Construct).length &&
|
||||||
!getIndexTypeOfType(type, IndexKind.String) &&
|
!getIndexTypeOfType(type, IndexKind.String) &&
|
||||||
!getIndexTypeOfType(type, IndexKind.Number);
|
!getIndexTypeOfType(type, IndexKind.Number);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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: 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; }'.
|
!!! 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
|
||||||
|
|
|
@ -31,6 +31,16 @@ function del(options: ConfigurableStartEnd = {},
|
||||||
changes.push(options);
|
changes.push(options);
|
||||||
changes.push(error);
|
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]
|
//// [weakType.js]
|
||||||
|
@ -48,3 +58,9 @@ function del(options, error) {
|
||||||
changes.push(options);
|
changes.push(options);
|
||||||
changes.push(error);
|
changes.push(error);
|
||||||
}
|
}
|
||||||
|
var K = (function () {
|
||||||
|
function K(s) {
|
||||||
|
}
|
||||||
|
return K;
|
||||||
|
}());
|
||||||
|
var ctor = K;
|
||||||
|
|
|
@ -30,3 +30,13 @@ function del(options: ConfigurableStartEnd = {},
|
||||||
changes.push(options);
|
changes.push(options);
|
||||||
changes.push(error);
|
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
|
||||||
|
|
Loading…
Reference in a new issue