diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3baa5fe623d..476695e3ec4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6030,7 +6030,7 @@ namespace ts { if (type.flags & TypeFlags.ObjectType) { const resolved = resolveStructuredTypeMembers(type); if ((relation === assignableRelation || relation === comparableRelation) && - (type === globalObjectType || resolved.properties.length === 0) || + (type === globalObjectType || isEmptyObjectType(resolved)) || resolved.stringIndexInfo || resolved.numberIndexInfo || getPropertyOfType(type, name)) { return true; } @@ -6045,6 +6045,14 @@ namespace ts { return false; } + function isEmptyObjectType(t: ResolvedType) { + return t.properties.length === 0 && + t.callSignatures.length === 0 && + t.constructSignatures.length === 0 && + !t.stringIndexInfo && + !t.numberIndexInfo; + } + function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean { if (!(target.flags & TypeFlags.ObjectLiteralPatternWithComputedProperties) && maybeTypeOfKind(target, TypeFlags.ObjectType)) { for (const prop of getPropertiesOfObjectType(source)) { diff --git a/tests/baselines/reference/excessPropertyErrorForFunctionTypes.errors.txt b/tests/baselines/reference/excessPropertyErrorForFunctionTypes.errors.txt new file mode 100644 index 00000000000..dc9d6821e55 --- /dev/null +++ b/tests/baselines/reference/excessPropertyErrorForFunctionTypes.errors.txt @@ -0,0 +1,12 @@ +tests/cases/compiler/excessPropertyErrorForFunctionTypes.ts(4,44): error TS2322: Type '{ a: number; c: number; d: number; }' is not assignable to type '{ a: number; c: number; } | (() => any)'. + Object literal may only specify known properties, and 'd' does not exist in type '{ a: number; c: number; } | (() => any)'. + + +==== tests/cases/compiler/excessPropertyErrorForFunctionTypes.ts (1 errors) ==== + type FunctionType = () => any; + type DoesntWork = { a: number, c: number } | FunctionType; + + let doesntWork: DoesntWork = { a: 1, c: 2, d: 3 } + ~~~~ +!!! error TS2322: Type '{ a: number; c: number; d: number; }' is not assignable to type '{ a: number; c: number; } | (() => any)'. +!!! error TS2322: Object literal may only specify known properties, and 'd' does not exist in type '{ a: number; c: number; } | (() => any)'. \ No newline at end of file diff --git a/tests/baselines/reference/excessPropertyErrorForFunctionTypes.js b/tests/baselines/reference/excessPropertyErrorForFunctionTypes.js new file mode 100644 index 00000000000..d81608c79ba --- /dev/null +++ b/tests/baselines/reference/excessPropertyErrorForFunctionTypes.js @@ -0,0 +1,8 @@ +//// [excessPropertyErrorForFunctionTypes.ts] +type FunctionType = () => any; +type DoesntWork = { a: number, c: number } | FunctionType; + +let doesntWork: DoesntWork = { a: 1, c: 2, d: 3 } + +//// [excessPropertyErrorForFunctionTypes.js] +var doesntWork = { a: 1, c: 2, d: 3 }; diff --git a/tests/cases/compiler/excessPropertyErrorForFunctionTypes.ts b/tests/cases/compiler/excessPropertyErrorForFunctionTypes.ts new file mode 100644 index 00000000000..d86d6ddb77c --- /dev/null +++ b/tests/cases/compiler/excessPropertyErrorForFunctionTypes.ts @@ -0,0 +1,4 @@ +type FunctionType = () => any; +type DoesntWork = { a: number, c: number } | FunctionType; + +let doesntWork: DoesntWork = { a: 1, c: 2, d: 3 } \ No newline at end of file