respond to code review remarks

This commit is contained in:
Mohamed Hegazy
2014-09-05 16:41:37 -07:00
parent 5422e56d48
commit 3b6afb8499
14 changed files with 86 additions and 74 deletions

View File

@@ -3132,7 +3132,7 @@ module ts {
return (type.flags & TypeFlags.Anonymous) && type.symbol && (type.symbol.flags & SymbolFlags.ObjectLiteral) ? true : false;
}
function getWidenedTypeOfObjectLiteral(type: Type, supressNoImplictAnyErrors?: boolean): Type {
function getWidenedTypeOfObjectLiteral(type: Type, supressNoImplicitAnyErrors?: boolean): Type {
var properties = getPropertiesOfType(type);
if (properties.length) {
var widenedTypes: Type[] = [];
@@ -3143,7 +3143,7 @@ module ts {
if (propType !== widenedType) {
propTypeWasWidened = true;
if (!supressNoImplictAnyErrors && program.getCompilerOptions().noImplicitAny && getInnermostTypeOfNestedArrayTypes(widenedType) === anyType) {
if (!supressNoImplicitAnyErrors && program.getCompilerOptions().noImplicitAny && getInnermostTypeOfNestedArrayTypes(widenedType) === anyType) {
error(p.valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(widenedType));
}
}
@@ -3183,9 +3183,9 @@ module ts {
return type;
}
function getWidenedTypeOfArrayLiteral(type: Type, supressNoImplictAnyErrors?: boolean): Type {
function getWidenedTypeOfArrayLiteral(type: Type, supressNoImplicitAnyErrors?: boolean): Type {
var elementType = (<TypeReference>type).typeArguments[0];
var widenedType = getWidenedType(elementType, supressNoImplictAnyErrors);
var widenedType = getWidenedType(elementType, supressNoImplicitAnyErrors);
type = elementType !== widenedType ? createArrayType(widenedType) : type;
@@ -3193,15 +3193,15 @@ module ts {
}
/* If we are widening on a literal, then we may need to the 'node' parameter for reporting purposes */
function getWidenedType(type: Type, supressNoImplictAnyErrors?: boolean): Type {
function getWidenedType(type: Type, supressNoImplicitAnyErrors?: boolean): Type {
if (type.flags & (TypeFlags.Undefined | TypeFlags.Null)) {
return anyType;
}
if (isTypeOfObjectLiteral(type)) {
return getWidenedTypeOfObjectLiteral(type, supressNoImplictAnyErrors);
return getWidenedTypeOfObjectLiteral(type, supressNoImplicitAnyErrors);
}
if (isArrayType(type)) {
return getWidenedTypeOfArrayLiteral(type, supressNoImplictAnyErrors);
return getWidenedTypeOfArrayLiteral(type, supressNoImplicitAnyErrors);
}
return type;
}
@@ -4330,10 +4330,7 @@ module ts {
var targetType = getTypeFromTypeNode(node.type);
if (fullTypeCheck && targetType !== unknownType) {
if (!isTypeAssignableTo(exprType, targetType)) {
var widenedType = getWidenedType(exprType, /*supressNoImplictAnyErrors*/ true);
if (!isTypeAssignableTo(targetType, widenedType)) {
checkTypeAssignableTo(targetType, widenedType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other);
}
checkTypeAssignableTo(targetType, getWidenedType(exprType, /*supressNoImplicitAnyErrors*/ true), node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other);
}
}
return targetType;

View File

@@ -4,7 +4,8 @@
<{ id: number; }[]>[{ foo: "s" }];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! Neither type '{ id: number; }[]' nor type '{ foo: string; }[]' is assignable to the other:
!!! Type '{ id: number; }' is not assignable to type '{ foo: string; }'.
!!! Type '{ id: number; }' is not assignable to type '{ foo: string; }':
!!! Property 'foo' is missing in type '{ id: number; }'.
// Should succeed, as the {} element causes the type of the array to be {}[]
<{ id: number; }[]>[{ foo: "s" }, {}];

View File

@@ -1,4 +1,5 @@
==== tests/cases/compiler/contextualTyping39.ts (1 errors) ====
var foo = <{ (): number; }> function() { return "err"; };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! Neither type '() => number' nor type '() => string' is assignable to the other.
!!! Neither type '() => number' nor type '() => string' is assignable to the other:
!!! Type 'number' is not assignable to type 'string'.

View File

@@ -1,4 +1,5 @@
==== tests/cases/compiler/contextualTyping41.ts (1 errors) ====
var foo = <{():number; (i:number):number; }> (function(){return "err";});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! Neither type '{ (): number; (i: number): number; }' nor type '() => string' is assignable to the other.
!!! Neither type '{ (): number; (i: number): number; }' nor type '() => string' is assignable to the other:
!!! Type 'number' is not assignable to type 'string'.

View File

@@ -32,7 +32,10 @@
worksToo():R {
return <R>({ oneI: this });
~~~~~~~~~~~~~~~~~~~
!!! Neither type 'R' nor type '{ oneI: C; }' is assignable to the other.
!!! Neither type 'R' nor type '{ oneI: C; }' is assignable to the other:
!!! Types of property 'oneI' are incompatible:
!!! Type 'I' is not assignable to type 'C':
!!! Property 'x' is missing in type 'I'.
}
}
}

View File

@@ -22,4 +22,5 @@
var r4: A<number> = <A<number>>new A();
var r5: A<number> = <A<number>>[]; // error
~~~~~~~~~~~~~
!!! Neither type 'A<number>' nor type 'any[]' is assignable to the other.
!!! Neither type 'A<number>' nor type 'any[]' is assignable to the other:
!!! Property 'length' is missing in type 'A<number>'.

View File

@@ -29,8 +29,10 @@
y = <T>a;
y = <T>b; // error: cannot convert B to T
~~~~
!!! Neither type 'T' nor type 'B' is assignable to the other.
!!! Neither type 'T' nor type 'B' is assignable to the other:
!!! Property 'bar' is missing in type 'A'.
y = <T>c; // error: cannot convert C to T
~~~~
!!! Neither type 'T' nor type 'C' is assignable to the other.
!!! Neither type 'T' nor type 'C' is assignable to the other:
!!! Property 'baz' is missing in type 'A'.
}

View File

@@ -29,8 +29,10 @@
y = <T>a;
y = <T>b; // error: cannot convert B to T
~~~~
!!! Neither type 'T' nor type 'B' is assignable to the other.
!!! Neither type 'T' nor type 'B' is assignable to the other:
!!! Property 'bar' is missing in type 'A'.
y = <T>c; // error: cannot convert C to T
~~~~
!!! Neither type 'T' nor type 'C' is assignable to the other.
!!! Neither type 'T' nor type 'C' is assignable to the other:
!!! Property 'baz' is missing in type 'A'.
}

View File

@@ -315,7 +315,8 @@
var obj69: i7 = new obj66;
var obj70: i7 = <i7>new Base;
~~~~~~~~~~~~
!!! Neither type 'i7' nor type 'Base' is assignable to the other.
!!! Neither type 'i7' nor type 'Base' is assignable to the other:
!!! Property 'foo' is missing in type 'i7'.
var obj71: i7 = null;
var obj72: i7 = function () { };
~~~~~

View File

@@ -0,0 +1,20 @@
==== tests/cases/compiler/noImplicitAnyInCastExpression.ts (1 errors) ====
// verify no noImplictAny errors reported with cast expression
interface IFoo {
a: number;
b: string;
}
// Expr type not assignable to target type
(<IFoo>{ a: null });
// Expr type assignanle to target type
(<IFoo>{ a: 2, b: undefined });
// Niether types is assignable to each other
(<IFoo>{ c: null });
~~~~~~~~~~~~~~~~~
!!! Neither type 'IFoo' nor type '{ c: any; }' is assignable to the other:
!!! Property 'c' is missing in type 'IFoo'.

View File

@@ -1,20 +1,26 @@
//// [noImplicitAnyInCastExpression.ts]
interface IBar {
b: number;
}
// verify no noImplictAny errors reported with cast expression
interface IFoo {
p: IBar[];
a: number;
b: string;
}
function foo(a: any) { }
// Expr type not assignable to target type
(<IFoo>{ a: null });
foo(<IFoo> {
p: null,
});
// Expr type assignanle to target type
(<IFoo>{ a: 2, b: undefined });
// Niether types is assignable to each other
(<IFoo>{ c: null });
//// [noImplicitAnyInCastExpression.js]
function foo(a) {
}
foo({
p: null
});
// verify no noImplictAny errors reported with cast expression
// Expr type not assignable to target type
{ a: null };
// Expr type assignanle to target type
{ a: 2, b: undefined };
// Niether types is assignable to each other
{ c: null };

View File

@@ -1,30 +0,0 @@
=== tests/cases/compiler/noImplicitAnyInCastExpression.ts ===
interface IBar {
>IBar : IBar
b: number;
>b : number
}
interface IFoo {
>IFoo : IFoo
p: IBar[];
>p : IBar[]
>IBar : IBar
}
function foo(a: any) { }
>foo : (a: any) => void
>a : any
foo(<IFoo> {
>foo(<IFoo> { p: null,}) : void
>foo : (a: any) => void
><IFoo> { p: null,} : IFoo
>IFoo : IFoo
>{ p: null,} : { p: null; }
p: null,
>p : any
});

View File

@@ -33,13 +33,15 @@
someBase = <SomeBase>someBase;
someBase = <SomeBase>someOther; // Error
~~~~~~~~~~~~~~~~~~~
!!! Neither type 'SomeBase' nor type 'SomeOther' is assignable to the other.
!!! Neither type 'SomeBase' nor type 'SomeOther' is assignable to the other:
!!! Property 'q' is missing in type 'SomeBase'.
someDerived = <SomeDerived>someDerived;
someDerived = <SomeDerived>someBase;
someDerived = <SomeDerived>someOther; // Error
~~~~~~~~~~~~~~~~~~~~~~
!!! Neither type 'SomeDerived' nor type 'SomeOther' is assignable to the other.
!!! Neither type 'SomeDerived' nor type 'SomeOther' is assignable to the other:
!!! Property 'q' is missing in type 'SomeDerived'.
someOther = <SomeOther>someDerived; // Error
~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -1,12 +1,17 @@
interface IBar {
b: number;
}
//@noImplicitAny: true
// verify no noImplictAny errors reported with cast expression
interface IFoo {
p: IBar[];
a: number;
b: string;
}
function foo(a: any) { }
// Expr type not assignable to target type
(<IFoo>{ a: null });
foo(<IFoo> {
p: null,
});
// Expr type assignanle to target type
(<IFoo>{ a: 2, b: undefined });
// Niether types is assignable to each other
(<IFoo>{ c: null });