Merge pull request #1033 from Microsoft/type_aliases_conformance

Type aliases conformance tests, fixed #1004
This commit is contained in:
Vladimir Matveev 2014-11-03 10:56:11 -08:00
commit 3c2f556306
10 changed files with 709 additions and 0 deletions

View File

@ -0,0 +1,72 @@
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(4,6): error TS2456: Type alias 'T0' circularly references itself.
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(6,6): error TS2456: Type alias 'T0_2' circularly references itself.
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(11,6): error TS2456: Type alias 'T1' circularly references itself.
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(14,6): error TS2456: Type alias 'T2' circularly references itself.
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(16,6): error TS2456: Type alias 'T2_1' circularly references itself.
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(19,6): error TS2456: Type alias 'T3' circularly references itself.
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(22,6): error TS2456: Type alias 'T4' circularly references itself.
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(26,6): error TS2456: Type alias 'T5' circularly references itself.
tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts(30,6): error TS2456: Type alias 'T7' circularly references itself.
==== tests/cases/conformance/types/typeAliases/directDependenceBetweenTypeAliases.ts (9 errors) ====
// It is an error for the type specified in a type alias to depend on that type alias
// A type alias directly depends on the type it aliases.
type T0 = T0
~~
!!! error TS2456: Type alias 'T0' circularly references itself.
type T0_1 = T0_2
type T0_2 = T0_3
~~~~
!!! error TS2456: Type alias 'T0_2' circularly references itself.
type T0_3 = T0_1
// A type reference directly depends on the referenced type and each of the type arguments, if any.
interface I<T> {}
type T1 = I<T1>
~~
!!! error TS2456: Type alias 'T1' circularly references itself.
// A union type directly depends on each of the constituent types.
type T2 = T2 | string
~~
!!! error TS2456: Type alias 'T2' circularly references itself.
class C<T> {}
type T2_1 = T2_1[] | number
~~~~
!!! error TS2456: Type alias 'T2_1' circularly references itself.
// An array type directly depends on its element type.
type T3 = T3[]
~~
!!! error TS2456: Type alias 'T3' circularly references itself.
// A tuple type directly depends on each of its element types.
type T4 = [number, T4]
~~
!!! error TS2456: Type alias 'T4' circularly references itself.
// A type query directly depends on the type of the referenced entity.
var x: T5[] = []
type T5 = typeof x
~~
!!! error TS2456: Type alias 'T5' circularly references itself.
class C1<T> {}
type T6 = T7 | number
type T7 = typeof yy
~~
!!! error TS2456: Type alias 'T7' circularly references itself.
var yy: [string, T8[]];
type T8 = C<T6>
// legal cases
type T9 = () => T9
type T10 = { x: T10 } | { new(v: T10): string }
type T11 = T12[]
type T12 = [T13, string]
type T13 = typeof zz
var zz: { x: T11 }

View File

@ -0,0 +1,60 @@
//// [directDependenceBetweenTypeAliases.ts]
// It is an error for the type specified in a type alias to depend on that type alias
// A type alias directly depends on the type it aliases.
type T0 = T0
type T0_1 = T0_2
type T0_2 = T0_3
type T0_3 = T0_1
// A type reference directly depends on the referenced type and each of the type arguments, if any.
interface I<T> {}
type T1 = I<T1>
// A union type directly depends on each of the constituent types.
type T2 = T2 | string
class C<T> {}
type T2_1 = T2_1[] | number
// An array type directly depends on its element type.
type T3 = T3[]
// A tuple type directly depends on each of its element types.
type T4 = [number, T4]
// A type query directly depends on the type of the referenced entity.
var x: T5[] = []
type T5 = typeof x
class C1<T> {}
type T6 = T7 | number
type T7 = typeof yy
var yy: [string, T8[]];
type T8 = C<T6>
// legal cases
type T9 = () => T9
type T10 = { x: T10 } | { new(v: T10): string }
type T11 = T12[]
type T12 = [T13, string]
type T13 = typeof zz
var zz: { x: T11 }
//// [directDependenceBetweenTypeAliases.js]
// It is an error for the type specified in a type alias to depend on that type alias
var C = (function () {
function C() {
}
return C;
})();
// A type query directly depends on the type of the referenced entity.
var x = [];
var C1 = (function () {
function C1() {
}
return C1;
})();
var yy;
var zz;

View File

@ -0,0 +1,30 @@
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(6,6): error TS1003: Identifier expected.
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(6,11): error TS1005: ';' expected.
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(2,6): error TS2457: Type alias name cannot be 'any'
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(3,6): error TS2457: Type alias name cannot be 'number'
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(4,6): error TS2457: Type alias name cannot be 'boolean'
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(5,6): error TS2457: Type alias name cannot be 'string'
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(6,13): error TS2304: Cannot find name 'I'.
==== tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts (7 errors) ====
interface I {}
type any = I;
~~~
!!! error TS2457: Type alias name cannot be 'any'
type number = I;
~~~~~~
!!! error TS2457: Type alias name cannot be 'number'
type boolean = I;
~~~~~~~
!!! error TS2457: Type alias name cannot be 'boolean'
type string = I;
~~~~~~
!!! error TS2457: Type alias name cannot be 'string'
type void = I;
~~~~
!!! error TS1003: Identifier expected.
~
!!! error TS1005: ';' expected.
~
!!! error TS2304: Cannot find name 'I'.

View File

@ -0,0 +1,126 @@
//// [typeAliases.ts]
// Writing a reference to a type alias has exactly the same effect as writing the aliased type itself.
type T1 = number;
var x1: number;
var x1: T1;
type T2 = string;
var x2: string;
var x2: T2;
type T3 = boolean;
var x3: boolean;
var x3: T3;
type T4 = void;
var x4: void;
var x4: T4;
type T5 = any;
var x5: any;
var x5: T5;
interface I6 { x : string }
type T6 = I6;
var x6: I6;
var x6: T6;
class C7 { x: boolean }
type T7 = C7;
var x7: C7;
var x7: T7;
type T8 = string | boolean;
var x8: string | boolean;
var x8: T8;
type T9 = () => string;
var x9: () => string;
var x9: T9;
type T10 = { x: number };
var x10: { x: number };
var x10: T10;
type T11 = { new(): boolean };
var x11: { new(): boolean };
var x11: T11;
interface I13 { x: string };
type T13 = I13;
var x13_1: I13;
var x13_2: T13
declare function foo13<T1 extends I13, T2 extends T13>(t1: T1, t2: T13): void;
foo13(x13_1, x13_2);
foo13(x13_2, x13_1);
type T14 = string;
var x14: T14;
declare function foo14_1(x: T14): void;
declare function foo14_2(x: "click"): void;
declare function foo14_2(x: T14): void;
type Meters = number
enum E { x = 10 }
declare function f15(a: string): boolean;
declare function f15(a: Meters): string;
f15(E.x).toLowerCase();
type StringAndBoolean = [string, boolean]
declare function f16(s: StringAndBoolean): string;
var x: [string, boolean];
f16(x);
var y: StringAndBoolean = ["1", false];
y[0].toLowerCase();
//// [typeAliases.js]
// Writing a reference to a type alias has exactly the same effect as writing the aliased type itself.
var x1;
var x1;
var x2;
var x2;
var x3;
var x3;
var x4;
var x4;
var x5;
var x5;
var x6;
var x6;
var C7 = (function () {
function C7() {
}
return C7;
})();
var x7;
var x7;
var x8;
var x8;
var x9;
var x9;
var x10;
var x10;
var x11;
var x11;
;
var x13_1;
var x13_2;
foo13(x13_1, x13_2);
foo13(x13_2, x13_1);
var x14;
var E;
(function (E) {
E[E["x"] = 10] = "x";
})(E || (E = {}));
f15(10 /* x */).toLowerCase();
var x;
f16(x);
var y = ["1", false];
y[0].toLowerCase();

View File

@ -0,0 +1,241 @@
=== tests/cases/conformance/types/typeAliases/typeAliases.ts ===
// Writing a reference to a type alias has exactly the same effect as writing the aliased type itself.
type T1 = number;
>T1 : number
var x1: number;
>x1 : number
var x1: T1;
>x1 : number
>T1 : number
type T2 = string;
>T2 : string
var x2: string;
>x2 : string
var x2: T2;
>x2 : string
>T2 : string
type T3 = boolean;
>T3 : boolean
var x3: boolean;
>x3 : boolean
var x3: T3;
>x3 : boolean
>T3 : boolean
type T4 = void;
>T4 : void
var x4: void;
>x4 : void
var x4: T4;
>x4 : void
>T4 : void
type T5 = any;
>T5 : any
var x5: any;
>x5 : any
var x5: T5;
>x5 : any
>T5 : any
interface I6 { x : string }
>I6 : I6
>x : string
type T6 = I6;
>T6 : I6
>I6 : I6
var x6: I6;
>x6 : I6
>I6 : I6
var x6: T6;
>x6 : I6
>T6 : I6
class C7 { x: boolean }
>C7 : C7
>x : boolean
type T7 = C7;
>T7 : C7
>C7 : C7
var x7: C7;
>x7 : C7
>C7 : C7
var x7: T7;
>x7 : C7
>T7 : C7
type T8 = string | boolean;
>T8 : string | boolean
var x8: string | boolean;
>x8 : string | boolean
var x8: T8;
>x8 : string | boolean
>T8 : string | boolean
type T9 = () => string;
>T9 : () => string
var x9: () => string;
>x9 : () => string
var x9: T9;
>x9 : () => string
>T9 : () => string
type T10 = { x: number };
>T10 : { x: number; }
>x : number
var x10: { x: number };
>x10 : { x: number; }
>x : number
var x10: T10;
>x10 : { x: number; }
>T10 : { x: number; }
type T11 = { new(): boolean };
>T11 : new () => boolean
var x11: { new(): boolean };
>x11 : new () => boolean
var x11: T11;
>x11 : new () => boolean
>T11 : new () => boolean
interface I13 { x: string };
>I13 : I13
>x : string
type T13 = I13;
>T13 : I13
>I13 : I13
var x13_1: I13;
>x13_1 : I13
>I13 : I13
var x13_2: T13
>x13_2 : I13
>T13 : I13
declare function foo13<T1 extends I13, T2 extends T13>(t1: T1, t2: T13): void;
>foo13 : <T1 extends I13, T2 extends I13>(t1: T1, t2: I13) => void
>T1 : T1
>I13 : I13
>T2 : T2
>T13 : I13
>t1 : T1
>T1 : T1
>t2 : I13
>T13 : I13
foo13(x13_1, x13_2);
>foo13(x13_1, x13_2) : void
>foo13 : <T1 extends I13, T2 extends I13>(t1: T1, t2: I13) => void
>x13_1 : I13
>x13_2 : I13
foo13(x13_2, x13_1);
>foo13(x13_2, x13_1) : void
>foo13 : <T1 extends I13, T2 extends I13>(t1: T1, t2: I13) => void
>x13_2 : I13
>x13_1 : I13
type T14 = string;
>T14 : string
var x14: T14;
>x14 : string
>T14 : string
declare function foo14_1(x: T14): void;
>foo14_1 : (x: string) => void
>x : string
>T14 : string
declare function foo14_2(x: "click"): void;
>foo14_2 : { (x: "click"): void; (x: string): void; }
>x : "click"
declare function foo14_2(x: T14): void;
>foo14_2 : { (x: "click"): void; (x: string): void; }
>x : string
>T14 : string
type Meters = number
>Meters : number
enum E { x = 10 }
>E : E
>x : E
declare function f15(a: string): boolean;
>f15 : { (a: string): boolean; (a: number): string; }
>a : string
declare function f15(a: Meters): string;
>f15 : { (a: string): boolean; (a: number): string; }
>a : number
>Meters : number
f15(E.x).toLowerCase();
>f15(E.x).toLowerCase() : string
>f15(E.x).toLowerCase : () => string
>f15(E.x) : string
>f15 : { (a: string): boolean; (a: number): string; }
>E.x : E
>E : typeof E
>x : E
>toLowerCase : () => string
type StringAndBoolean = [string, boolean]
>StringAndBoolean : [string, boolean]
declare function f16(s: StringAndBoolean): string;
>f16 : (s: [string, boolean]) => string
>s : [string, boolean]
>StringAndBoolean : [string, boolean]
var x: [string, boolean];
>x : [string, boolean]
f16(x);
>f16(x) : string
>f16 : (s: [string, boolean]) => string
>x : [string, boolean]
var y: StringAndBoolean = ["1", false];
>y : [string, boolean]
>StringAndBoolean : [string, boolean]
>["1", false] : [string, boolean]
y[0].toLowerCase();
>y[0].toLowerCase() : string
>y[0].toLowerCase : () => string
>y[0] : string
>y : [string, boolean]
>toLowerCase : () => string

View File

@ -0,0 +1,39 @@
tests/cases/conformance/types/typeAliases/typeAliasesForObjectTypes.ts(14,8): error TS1005: '=' expected.
tests/cases/conformance/types/typeAliases/typeAliasesForObjectTypes.ts(14,12): error TS1005: '(' expected.
tests/cases/conformance/types/typeAliases/typeAliasesForObjectTypes.ts(4,22): error TS2312: An interface may only extend a class or another interface.
tests/cases/conformance/types/typeAliases/typeAliasesForObjectTypes.ts(5,21): error TS2422: A class may only implement another class or interface.
tests/cases/conformance/types/typeAliases/typeAliasesForObjectTypes.ts(10,6): error TS2300: Duplicate identifier 'T2'.
tests/cases/conformance/types/typeAliases/typeAliasesForObjectTypes.ts(11,6): error TS2300: Duplicate identifier 'T2'.
tests/cases/conformance/types/typeAliases/typeAliasesForObjectTypes.ts(14,19): error TS2304: Cannot find name 'T'.
==== tests/cases/conformance/types/typeAliases/typeAliasesForObjectTypes.ts (7 errors) ====
type T1 = { x: string }
// An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
interface I1 extends T1 { y: string }
~~
!!! error TS2312: An interface may only extend a class or another interface.
class C1 implements T1 {
~~
!!! error TS2422: A class may only implement another class or interface.
x: string;
}
// An interface can have multiple merged declarations, but a type alias for an object type literal cannot.
type T2 = { x: string }
~~
!!! error TS2300: Duplicate identifier 'T2'.
type T2 = { y: number }
~~
!!! error TS2300: Duplicate identifier 'T2'.
// An interface can have type parameters, but a type alias for an object type literal cannot.
type T3<T> = { x: T }
~
!!! error TS1005: '=' expected.
~
!!! error TS1005: '(' expected.
~
!!! error TS2304: Cannot find name 'T'.

View File

@ -0,0 +1,41 @@
// It is an error for the type specified in a type alias to depend on that type alias
// A type alias directly depends on the type it aliases.
type T0 = T0
type T0_1 = T0_2
type T0_2 = T0_3
type T0_3 = T0_1
// A type reference directly depends on the referenced type and each of the type arguments, if any.
interface I<T> {}
type T1 = I<T1>
// A union type directly depends on each of the constituent types.
type T2 = T2 | string
class C<T> {}
type T2_1 = T2_1[] | number
// An array type directly depends on its element type.
type T3 = T3[]
// A tuple type directly depends on each of its element types.
type T4 = [number, T4]
// A type query directly depends on the type of the referenced entity.
var x: T5[] = []
type T5 = typeof x
class C1<T> {}
type T6 = T7 | number
type T7 = typeof yy
var yy: [string, T8[]];
type T8 = C<T6>
// legal cases
type T9 = () => T9
type T10 = { x: T10 } | { new(v: T10): string }
type T11 = T12[]
type T12 = [T13, string]
type T13 = typeof zz
var zz: { x: T11 }

View File

@ -0,0 +1,6 @@
interface I {}
type any = I;
type number = I;
type boolean = I;
type string = I;
type void = I;

View File

@ -0,0 +1,80 @@
// Writing a reference to a type alias has exactly the same effect as writing the aliased type itself.
type T1 = number;
var x1: number;
var x1: T1;
type T2 = string;
var x2: string;
var x2: T2;
type T3 = boolean;
var x3: boolean;
var x3: T3;
type T4 = void;
var x4: void;
var x4: T4;
type T5 = any;
var x5: any;
var x5: T5;
interface I6 { x : string }
type T6 = I6;
var x6: I6;
var x6: T6;
class C7 { x: boolean }
type T7 = C7;
var x7: C7;
var x7: T7;
type T8 = string | boolean;
var x8: string | boolean;
var x8: T8;
type T9 = () => string;
var x9: () => string;
var x9: T9;
type T10 = { x: number };
var x10: { x: number };
var x10: T10;
type T11 = { new(): boolean };
var x11: { new(): boolean };
var x11: T11;
interface I13 { x: string };
type T13 = I13;
var x13_1: I13;
var x13_2: T13
declare function foo13<T1 extends I13, T2 extends T13>(t1: T1, t2: T13): void;
foo13(x13_1, x13_2);
foo13(x13_2, x13_1);
type T14 = string;
var x14: T14;
declare function foo14_1(x: T14): void;
declare function foo14_2(x: "click"): void;
declare function foo14_2(x: T14): void;
type Meters = number
enum E { x = 10 }
declare function f15(a: string): boolean;
declare function f15(a: Meters): string;
f15(E.x).toLowerCase();
type StringAndBoolean = [string, boolean]
declare function f16(s: StringAndBoolean): string;
var x: [string, boolean];
f16(x);
var y: StringAndBoolean = ["1", false];
y[0].toLowerCase();

View File

@ -0,0 +1,14 @@
type T1 = { x: string }
// An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
interface I1 extends T1 { y: string }
class C1 implements T1 {
x: string;
}
// An interface can have multiple merged declarations, but a type alias for an object type literal cannot.
type T2 = { x: string }
type T2 = { y: number }
// An interface can have type parameters, but a type alias for an object type literal cannot.
type T3<T> = { x: T }