diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ff716c54db..e43001ec43 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); } diff --git a/tests/baselines/reference/weakType.errors.txt b/tests/baselines/reference/weakType.errors.txt index 532092eb94..8d0da84859 100644 --- a/tests/baselines/reference/weakType.errors.txt +++ b/tests/baselines/reference/weakType.errors.txt @@ -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 \ No newline at end of file diff --git a/tests/baselines/reference/weakType.js b/tests/baselines/reference/weakType.js index 0ca408b365..365b45d68c 100644 --- a/tests/baselines/reference/weakType.js +++ b/tests/baselines/reference/weakType.js @@ -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; diff --git a/tests/cases/compiler/weakType.ts b/tests/cases/compiler/weakType.ts index 363a7da9be..a1d1cae7f6 100644 --- a/tests/cases/compiler/weakType.ts +++ b/tests/cases/compiler/weakType.ts @@ -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