Merge pull request #8697 from Microsoft/Fix8694

Fix #8694: check for singatures before calling a type empty
This commit is contained in:
Mohamed Hegazy 2016-05-19 16:14:57 -07:00
commit 0275fe25d7
4 changed files with 33 additions and 1 deletions

View file

@ -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)) {

View file

@ -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)'.

View file

@ -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 };

View file

@ -0,0 +1,4 @@
type FunctionType = () => any;
type DoesntWork = { a: number, c: number } | FunctionType;
let doesntWork: DoesntWork = { a: 1, c: 2, d: 3 }