Skip to content

Commit

Permalink
ts2.1: handle tuple types
Browse files Browse the repository at this point in the history
Restore some code that special-cased tuple types.
Add some more test cases around tuples.

Rebaseline one test, because we added more test cases.
Note that this rebaseline also changes the type of {},
the empty object; I don't think it matters too much.

More work on #295. This fixes a crash in the "type" test.
  • Loading branch information
evmar committed Jan 10, 2017
1 parent 4b4bb85 commit cfd08d9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/type-translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ export class TypeTranslator {
// Emit the referenced type and any type arguments.
let referenceType = type as ts.TypeReference;

// A tuple is a ReferenceType where the target is flagged Tuple and the
// typeArguments are the tuple arguments. Just treat it as a mystery
// array, because Closure doesn't understand tuples.
if (referenceType.target.objectFlags & ts.ObjectFlags.Tuple) {
return '!Array<?>';
}

let typeStr = '';
if (referenceType.target === referenceType) {
// We get into an infinite loop here if the inner reference is
Expand Down
6 changes: 5 additions & 1 deletion test_files/type/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ let /** @type {!Array<!Array<{a: ?}>>} */ typeNestedArr;
let /** @type {{a: number, b: string}} */ typeObject = { a: 3, b: 'b' };
let /** @type {!Object<string,number>} */ typeObject2;
let /** @type {?} */ typeObject3;
let /** @type {!Object} */ typeObjectEmpty;
let /** @type {?} */ typeObjectEmpty;
let /** @type {!Array<?>} */ typeTuple = [1, 2];
let /** @type {!Array<?>} */ typeComplexTuple = ['', true];
let /** @type {!Array<?>} */ typeTupleTuple = [[1, 2]];
let /** @type {!Array<?>} */ typeTupleTuple2 = [[1, 2], ''];
let /** @type {(string|boolean)} */ typeUnion = Math.random() > 0.5 ? false : '';
let /** @type {(string|boolean)} */ typeUnion2 = Math.random() > 0.5 ? false : '';
let /** @type {{optional: (undefined|boolean)}} */ typeOptionalField = {};
Expand Down
6 changes: 6 additions & 0 deletions test_files/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ let typeAny: any;
let typeArr: Array<any>;
let typeArr2: any[];
let typeNestedArr: {a:any}[][];

let typeObject: {a:number, b:string} = {a:3, b:'b'};
let typeObject2: {[key:string]: number};
let typeObject3: {a:number, [key:string]: number};
let typeObjectEmpty: {};

let typeTuple: [number, number] = [1, 2];
let typeComplexTuple: [string, true|{a:string}] = ['', true];
let typeTupleTuple: [[number, number]] = [[1, 2]];
let typeTupleTuple2: [[number, number], string] = [[1, 2], ''];

let typeUnion: string|boolean = Math.random() > 0.5 ? false : '';
let typeUnion2: (string|boolean) = Math.random() > 0.5 ? false : '';
let typeOptionalField: {optional?: boolean} = {};
Expand Down
15 changes: 11 additions & 4 deletions test_files/type/type.tsickle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Warning at test_files/type/type.ts:13:5: unhandled type literal
Warning at test_files/type/type.ts:28:1: unhandled type {type flags:0x4000 TypeParameter symbol.name:"T"}
Warning at test_files/type/type.ts:28:1: unhandled type {type flags:0x4000 TypeParameter symbol.name:"T"}
Warning at test_files/type/type.ts:14:5: unhandled type literal
Warning at test_files/type/type.ts:15:5: symbol has no declarations
Warning at test_files/type/type.ts:34:1: unhandled type flags: TypeParameter
Warning at test_files/type/type.ts:34:1: unhandled type flags: TypeParameter
====
// Ensure we still understand what Array is, even when it has been
// monkeypatched -- issue #170.
Expand All @@ -12,10 +13,16 @@ let /** @type {?} */ typeAny: any;
let /** @type {!Array<?>} */ typeArr: Array<any>;
let /** @type {!Array<?>} */ typeArr2: any[];
let /** @type {!Array<!Array<{a: ?}>>} */ typeNestedArr: {a:any}[][];

let /** @type {{a: number, b: string}} */ typeObject: {a:number, b:string} = {a:3, b:'b'};
let /** @type {!Object<string,number>} */ typeObject2: {[key:string]: number};
let /** @type {?} */ typeObject3: {a:number, [key:string]: number};
let /** @type {!Object} */ typeObjectEmpty: {};
let /** @type {?} */ typeObjectEmpty: {};

let /** @type {!Array<?>} */ typeTuple: [number, number] = [1, 2];
let /** @type {!Array<?>} */ typeComplexTuple: [string, true|{a:string}] = ['', true];
let /** @type {!Array<?>} */ typeTupleTuple: [[number, number]] = [[1, 2]];
let /** @type {!Array<?>} */ typeTupleTuple2: [[number, number], string] = [[1, 2], ''];

let /** @type {(string|boolean)} */ typeUnion: string|boolean = Math.random() > 0.5 ? false : '';
let /** @type {(string|boolean)} */ typeUnion2: (string|boolean) = Math.random() > 0.5 ? false : '';
Expand Down

0 comments on commit cfd08d9

Please sign in to comment.