Fix widening in object literal property assignments

This commit is contained in:
Anders Hejlsberg 2014-09-10 12:58:30 -07:00
parent bf084f159d
commit 3d92adec7b
19 changed files with 38 additions and 35 deletions

View file

@ -1354,10 +1354,13 @@ module ts {
}
// Use the type of the initializer expression if one is present
if (declaration.initializer) {
var unwidenedType = checkAndMarkExpression(declaration.initializer);
var type = getWidenedType(unwidenedType);
if (type !== unwidenedType) {
checkImplicitAny(type);
var type = checkAndMarkExpression(declaration.initializer);
if (declaration.kind !== SyntaxKind.PropertyAssignment) {
var unwidenedType = type;
type = getWidenedType(type);
if (type !== unwidenedType) {
checkImplicitAny(type);
}
}
return type;
}

View file

@ -222,7 +222,7 @@ class f {
>base : base
>[ { x: undefined, y: new base() }, { x: '', y: new derived() } ] : { x: string; y: base; }[]
>{ x: undefined, y: new base() } : { x: undefined; y: base; }
>x : any
>x : undefined
>undefined : undefined
>y : base
>new base() : base

View file

@ -4,7 +4,7 @@
var n = { w: null, x: '', y: () => { }, z: 32 };
>n : { w: any; x: string; y: () => void; z: number; }
>{ w: null, x: '', y: () => { }, z: 32 } : { w: null; x: string; y: () => void; z: number; }
>w : any
>w : null
>x : string
>y : () => void
>() => { } : () => void

View file

@ -16,9 +16,9 @@ class bar {
>a : bar
><bar>null : bar
>bar : bar
>b : any
>b : undefined
>undefined : undefined
>c : any
>c : undefined
>void 4 : undefined
public h(x = 4, y = null, z = '') { x++; }

View file

@ -15,7 +15,7 @@ var obj = {x:1,y:null};
>obj : { x: number; y: any; }
>{x:1,y:null} : { x: number; y: null; }
>x : number
>y : any
>y : null
class A {
>A : A

View file

@ -26,5 +26,5 @@ var test: IIntervalTreeNode[] = [{ interval: { begin: 0 }, children: null }]; //
>interval : { begin: number; }
>{ begin: 0 } : { begin: number; }
>begin : number
>children : any
>children : null

View file

@ -39,7 +39,7 @@ for (var p: Point = { x: 0, y: undefined }; ;) { }
>Point : Point
>{ x: 0, y: undefined } : { x: number; y: undefined; }
>x : number
>y : any
>y : undefined
>undefined : undefined
for (var p = { x: 1, y: <number>undefined }; ;) { }
@ -65,7 +65,7 @@ for (var p = <{ x: number; y: number; }>{ x: 0, y: undefined }; ;) { }
>y : number
>{ x: 0, y: undefined } : { x: number; y: undefined; }
>x : number
>y : any
>y : undefined
>undefined : undefined
for (var p: typeof p; ;) { }

View file

@ -253,8 +253,8 @@ function opt2(n = { x: null, y: undefined }) {
>opt2 : (n?: { x: any; y: any; }) => void
>n : { x: any; y: any; }
>{ x: null, y: undefined } : { x: null; y: undefined; }
>x : any
>y : any
>x : null
>y : undefined
>undefined : undefined
var m = n;

View file

@ -15,7 +15,7 @@ var obj = {x:1,y:null};
>obj : { x: number; y: any; }
>{x:1,y:null} : { x: number; y: null; }
>x : number
>y : any
>y : null
class A {
>A : A

View file

@ -95,7 +95,7 @@ var a: Foo = {
>{} : {}
e: null ,
>e : any
>e : null
f: [1],
>f : number[]

View file

@ -41,7 +41,7 @@ var w:I={x:null,y:3};
>w : I
>I : I
>{x:null,y:3} : { x: null; y: number; }
>x : any
>x : null
>y : number

View file

@ -6,10 +6,10 @@ var x = {
>{ foo: null, bar: undefined} : { foo: null; bar: undefined; }
foo: null,
>foo : any
>foo : null
bar: undefined
>bar : any
>bar : undefined
>undefined : undefined
}
@ -18,17 +18,17 @@ var y = {
>{ foo: null, bar: { baz: null, boo: undefined }} : { foo: null; bar: { baz: null; boo: undefined; }; }
foo: null,
>foo : any
>foo : null
bar: {
>bar : { baz: any; boo: any; }
>bar : { baz: null; boo: undefined; }
>{ baz: null, boo: undefined } : { baz: null; boo: undefined; }
baz: null,
>baz : any
>baz : null
boo: undefined
>boo : any
>boo : undefined
>undefined : undefined
}
}

View file

@ -61,7 +61,7 @@ module Bugs {
>startIndex : number
>type : string
>bracket : number
>state : any
>state : null
>length : number
}
}

View file

@ -25,7 +25,7 @@ var b = {
>{ foo: null} : { foo: null; }
foo: null
>foo : any
>foo : null
}
// These should all be of type 'any'

View file

@ -31,7 +31,7 @@ var z3 = foo({ name: null }); // { name: any }
>foo({ name: null }) : { name: any; }
>foo : <T extends Item>(x?: T, y?: T) => T
>{ name: null } : { name: null; }
>name : any
>name : null
var z4 = foo({ name: "abc" }); // { name: string }
>z4 : { name: string; }

View file

@ -22,7 +22,7 @@ fn5({ x: null });
>fn5({ x: null }) : void
>fn5 : <T extends { x: string; }>(n: T) => void
>{ x: null } : { x: null; }
>x : any
>x : null
function fn6<T extends { x: string }>(n: T, fun: (x: T) => void, n2: T) { }
>fn6 : <T extends { x: string; }>(n: T, fun: (x: T) => void, n2: T) => void
@ -40,7 +40,7 @@ fn6({ x: null }, y => { }, { x: "" }); // y has type { x: any }, but ideally wou
>fn6({ x: null }, y => { }, { x: "" }) : void
>fn6 : <T extends { x: string; }>(n: T, fun: (x: T) => void, n2: T) => void
>{ x: null } : { x: null; }
>x : any
>x : null
>y => { } : (y: { x: string; }) => void
>y : { x: string; }
>{ x: "" } : { x: string; }

View file

@ -19,8 +19,8 @@ var z1 = foo1({ x: undefined, y: undefined });
>foo1({ x: undefined, y: undefined }) : any
>foo1 : <T>(f1: { x: T; y: T; }) => T
>{ x: undefined, y: undefined } : { x: undefined; y: undefined; }
>x : any
>x : undefined
>undefined : undefined
>y : any
>y : undefined
>undefined : undefined

View file

@ -47,7 +47,7 @@ var p: Point = { x: 0, y: undefined };
>Point : Point
>{ x: 0, y: undefined } : { x: number; y: undefined; }
>x : number
>y : any
>y : undefined
>undefined : undefined
var p = { x: 1, y: <number>undefined };
@ -73,7 +73,7 @@ var p = <{ x: number; y: number; }>{ x: 0, y: undefined };
>y : number
>{ x: 0, y: undefined } : { x: number; y: undefined; }
>x : number
>y : any
>y : undefined
>undefined : undefined
var p: typeof p;

View file

@ -9,13 +9,13 @@ var b = undefined;
var c = {x: null};
>c : { x: any; }
>{x: null} : { x: null; }
>x : any
>x : null
var d = [{x: null}];
>d : { x: any; }[]
>[{x: null}] : { x: null; }[]
>{x: null} : { x: null; }
>x : any
>x : null
var f = [null, null];
>f : any[]
@ -31,6 +31,6 @@ var h = [{x: undefined}];
>h : { x: any; }[]
>[{x: undefined}] : { x: undefined; }[]
>{x: undefined} : { x: undefined; }
>x : any
>x : undefined
>undefined : undefined