Merge pull request #3902 from Microsoft/tupleTypesAny

Don't fall back to any when typing tuples
This commit is contained in:
Jason Freeman 2015-07-17 14:22:33 -07:00
commit fbe60c93f4
18 changed files with 89 additions and 5 deletions

View File

@ -2274,10 +2274,6 @@ namespace ts {
// fact an iterable or array (depending on target language).
let elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false);
if (!declaration.dotDotDotToken) {
if (isTypeAny(elementType)) {
return elementType;
}
// Use specific property type when parent is a tuple or numeric index type when parent is an array
let propName = "" + indexOf(pattern.elements, declaration);
type = isTupleLikeType(parentType)

View File

@ -1,6 +1,7 @@
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(13,12): error TS2493: Tuple type '[string, number]' with length '2' cannot be assigned to tuple with length '3'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(14,12): error TS2460: Type 'StrNum' has no property '2'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,5): error TS2461: Type '{ 0: string; 1: number; }' is not an array type.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,12): error TS2460: Type '{ 0: string; 1: number; }' has no property '2'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(16,5): error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.
Types of property '0' are incompatible.
Type 'string' is not assignable to type 'number'.
@ -46,7 +47,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
Type 'string' is not assignable to type 'number'.
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (18 errors) ====
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (19 errors) ====
interface StrNum extends Array<string|number> {
0: string;
1: number;
@ -68,6 +69,8 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
var [g, h, i] = z;
~~~~~~~~~
!!! error TS2461: Type '{ 0: string; 1: number; }' is not an array type.
~
!!! error TS2460: Type '{ 0: string; 1: number; }' has no property '2'.
var j1: [number, number, number] = x;
~~
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.

View File

@ -0,0 +1,5 @@
//// [tupleElementTypes1.ts]
var [a, b]: [number, any] = [undefined, undefined];
//// [tupleElementTypes1.js]
var _a = [undefined, undefined], a = _a[0], b = _a[1];

View File

@ -0,0 +1,7 @@
=== tests/cases/conformance/types/tuple/tupleElementTypes1.ts ===
var [a, b]: [number, any] = [undefined, undefined];
>a : Symbol(a, Decl(tupleElementTypes1.ts, 0, 5))
>b : Symbol(b, Decl(tupleElementTypes1.ts, 0, 7))
>undefined : Symbol(undefined)
>undefined : Symbol(undefined)

View File

@ -0,0 +1,8 @@
=== tests/cases/conformance/types/tuple/tupleElementTypes1.ts ===
var [a, b]: [number, any] = [undefined, undefined];
>a : number
>b : any
>[undefined, undefined] : [undefined, undefined]
>undefined : undefined
>undefined : undefined

View File

@ -0,0 +1,7 @@
//// [tupleElementTypes2.ts]
function f([a, b]: [number, any]) { }
//// [tupleElementTypes2.js]
function f(_a) {
var a = _a[0], b = _a[1];
}

View File

@ -0,0 +1,6 @@
=== tests/cases/conformance/types/tuple/tupleElementTypes2.ts ===
function f([a, b]: [number, any]) { }
>f : Symbol(f, Decl(tupleElementTypes2.ts, 0, 0))
>a : Symbol(a, Decl(tupleElementTypes2.ts, 0, 12))
>b : Symbol(b, Decl(tupleElementTypes2.ts, 0, 14))

View File

@ -0,0 +1,6 @@
=== tests/cases/conformance/types/tuple/tupleElementTypes2.ts ===
function f([a, b]: [number, any]) { }
>f : ([a, b]: [number, any]) => void
>a : number
>b : any

View File

@ -0,0 +1,5 @@
//// [tupleElementTypes3.ts]
var [a, b] = [0, undefined];
//// [tupleElementTypes3.js]
var _a = [0, undefined], a = _a[0], b = _a[1];

View File

@ -0,0 +1,6 @@
=== tests/cases/conformance/types/tuple/tupleElementTypes3.ts ===
var [a, b] = [0, undefined];
>a : Symbol(a, Decl(tupleElementTypes3.ts, 0, 5))
>b : Symbol(b, Decl(tupleElementTypes3.ts, 0, 7))
>undefined : Symbol(undefined)

View File

@ -0,0 +1,8 @@
=== tests/cases/conformance/types/tuple/tupleElementTypes3.ts ===
var [a, b] = [0, undefined];
>a : number
>b : any
>[0, undefined] : [number, undefined]
>0 : number
>undefined : undefined

View File

@ -0,0 +1,7 @@
//// [tupleElementTypes4.ts]
function f([a, b] = [0, undefined]) { }
//// [tupleElementTypes4.js]
function f(_a) {
var _b = _a === void 0 ? [0, undefined] : _a, a = _b[0], b = _b[1];
}

View File

@ -0,0 +1,7 @@
=== tests/cases/conformance/types/tuple/tupleElementTypes4.ts ===
function f([a, b] = [0, undefined]) { }
>f : Symbol(f, Decl(tupleElementTypes4.ts, 0, 0))
>a : Symbol(a, Decl(tupleElementTypes4.ts, 0, 12))
>b : Symbol(b, Decl(tupleElementTypes4.ts, 0, 14))
>undefined : Symbol(undefined)

View File

@ -0,0 +1,9 @@
=== tests/cases/conformance/types/tuple/tupleElementTypes4.ts ===
function f([a, b] = [0, undefined]) { }
>f : ([a, b]?: [number, any]) => void
>a : number
>b : any
>[0, undefined] : [number, undefined]
>0 : number
>undefined : undefined

View File

@ -0,0 +1 @@
var [a, b]: [number, any] = [undefined, undefined];

View File

@ -0,0 +1 @@
function f([a, b]: [number, any]) { }

View File

@ -0,0 +1 @@
var [a, b] = [0, undefined];

View File

@ -0,0 +1 @@
function f([a, b] = [0, undefined]) { }