Tests for checking if type S is subtype of union type

T is a union type and S is a subtype of at least one constituent type of T
This commit is contained in:
Sheetal Nandi 2014-11-03 09:46:32 -08:00
parent a1535533e7
commit 703cd5d4cf
3 changed files with 279 additions and 0 deletions

View file

@ -0,0 +1,142 @@
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(16,5): error TS2411: Property 'foo4' of type 'boolean' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(18,5): error TS2411: Property 'foo6' of type 'Date' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(19,5): error TS2411: Property 'foo7' of type 'RegExp' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(20,5): error TS2411: Property 'foo8' of type '{ bar: number; }' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(21,5): error TS2411: Property 'foo9' of type 'I8' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(22,5): error TS2411: Property 'foo10' of type 'A' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(23,5): error TS2411: Property 'foo11' of type 'A2<number>' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(24,5): error TS2411: Property 'foo12' of type '(x: any) => number' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(25,5): error TS2411: Property 'foo13' of type '<T>(x: T) => T' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(26,5): error TS2411: Property 'foo14' of type 'typeof f' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(27,5): error TS2411: Property 'foo15' of type 'typeof c' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(28,5): error TS2411: Property 'foo16' of type 'T' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(29,5): error TS2411: Property 'foo17' of type 'Object' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(30,5): error TS2411: Property 'foo18' of type '{}' is not assignable to string index type 'string | number'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(35,5): error TS2411: Property 'foo2' of type 'string' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(37,5): error TS2411: Property 'foo4' of type 'boolean' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(39,5): error TS2411: Property 'foo6' of type 'Date' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(40,5): error TS2411: Property 'foo7' of type 'RegExp' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(41,5): error TS2411: Property 'foo8' of type '{ bar: number; }' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(42,5): error TS2411: Property 'foo9' of type 'I8' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(43,5): error TS2411: Property 'foo10' of type 'A' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(44,5): error TS2411: Property 'foo11' of type 'A2<number>' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(45,5): error TS2411: Property 'foo12' of type '(x: any) => number' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(46,5): error TS2411: Property 'foo13' of type '<T>(x: T) => T' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(47,5): error TS2411: Property 'foo14' of type 'typeof f' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(48,5): error TS2411: Property 'foo15' of type 'typeof c' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(49,5): error TS2411: Property 'foo16' of type 'T' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(50,5): error TS2411: Property 'foo17' of type 'Object' is not assignable to string index type 'number | E'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts(51,5): error TS2411: Property 'foo18' of type '{}' is not assignable to string index type 'number | E'.
==== tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts (29 errors) ====
enum E { e1, e2 }
interface I8 { [x: string]: number[]; }
class A { foo: number; }
class A2<T> { foo: T; }
function f() { }
module f { export var bar = 1; }
class c { baz: string }
module c { export var bar = 1; }
// A type T is a subtype of a union type U if T is a subtype of any type in U.
interface I1<T> {
[x: string]: string | number;
foo: any; // ok
foo2: string; // ok
foo3: number; // ok
foo4: boolean; // error
~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo4' of type 'boolean' is not assignable to string index type 'string | number'.
foo5: E; // ok - subtype of number
foo6: Date; // error
~~~~~~~~~~~
!!! error TS2411: Property 'foo6' of type 'Date' is not assignable to string index type 'string | number'.
foo7: RegExp; // error
~~~~~~~~~~~~~
!!! error TS2411: Property 'foo7' of type 'RegExp' is not assignable to string index type 'string | number'.
foo8: { bar: number }; // error
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo8' of type '{ bar: number; }' is not assignable to string index type 'string | number'.
foo9: I8; // error
~~~~~~~~~
!!! error TS2411: Property 'foo9' of type 'I8' is not assignable to string index type 'string | number'.
foo10: A; // error
~~~~~~~~~
!!! error TS2411: Property 'foo10' of type 'A' is not assignable to string index type 'string | number'.
foo11: A2<number>; // error
~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo11' of type 'A2<number>' is not assignable to string index type 'string | number'.
foo12: (x) => number; //error
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo12' of type '(x: any) => number' is not assignable to string index type 'string | number'.
foo13: <T>(x: T) => T; // error
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo13' of type '<T>(x: T) => T' is not assignable to string index type 'string | number'.
foo14: typeof f; // error
~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo14' of type 'typeof f' is not assignable to string index type 'string | number'.
foo15: typeof c; // error
~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo15' of type 'typeof c' is not assignable to string index type 'string | number'.
foo16: T; // error
~~~~~~~~~
!!! error TS2411: Property 'foo16' of type 'T' is not assignable to string index type 'string | number'.
foo17: Object; // error
~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo17' of type 'Object' is not assignable to string index type 'string | number'.
foo18: {}; // error
~~~~~~~~~~
!!! error TS2411: Property 'foo18' of type '{}' is not assignable to string index type 'string | number'.
}
interface I2<T> {
[x: string]: E | number;
foo: any; // ok
foo2: string; // error
~~~~~~~~~~~~~
!!! error TS2411: Property 'foo2' of type 'string' is not assignable to string index type 'number | E'.
foo3: number; // ok
foo4: boolean; // error
~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo4' of type 'boolean' is not assignable to string index type 'number | E'.
foo5: E; // ok
foo6: Date; // error
~~~~~~~~~~~
!!! error TS2411: Property 'foo6' of type 'Date' is not assignable to string index type 'number | E'.
foo7: RegExp; // error
~~~~~~~~~~~~~
!!! error TS2411: Property 'foo7' of type 'RegExp' is not assignable to string index type 'number | E'.
foo8: { bar: number }; // error
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo8' of type '{ bar: number; }' is not assignable to string index type 'number | E'.
foo9: I8; // error
~~~~~~~~~
!!! error TS2411: Property 'foo9' of type 'I8' is not assignable to string index type 'number | E'.
foo10: A; // error
~~~~~~~~~
!!! error TS2411: Property 'foo10' of type 'A' is not assignable to string index type 'number | E'.
foo11: A2<number>; // error
~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo11' of type 'A2<number>' is not assignable to string index type 'number | E'.
foo12: (x) => number; //error
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo12' of type '(x: any) => number' is not assignable to string index type 'number | E'.
foo13: <T>(x: T) => T; // error
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo13' of type '<T>(x: T) => T' is not assignable to string index type 'number | E'.
foo14: typeof f; // error
~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo14' of type 'typeof f' is not assignable to string index type 'number | E'.
foo15: typeof c; // error
~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo15' of type 'typeof c' is not assignable to string index type 'number | E'.
foo16: T; // error
~~~~~~~~~
!!! error TS2411: Property 'foo16' of type 'T' is not assignable to string index type 'number | E'.
foo17: Object; // error
~~~~~~~~~~~~~~
!!! error TS2411: Property 'foo17' of type 'Object' is not assignable to string index type 'number | E'.
foo18: {}; // error
~~~~~~~~~~
!!! error TS2411: Property 'foo18' of type '{}' is not assignable to string index type 'number | E'.
}

View file

@ -0,0 +1,85 @@
//// [subtypesOfUnion.ts]
enum E { e1, e2 }
interface I8 { [x: string]: number[]; }
class A { foo: number; }
class A2<T> { foo: T; }
function f() { }
module f { export var bar = 1; }
class c { baz: string }
module c { export var bar = 1; }
// A type T is a subtype of a union type U if T is a subtype of any type in U.
interface I1<T> {
[x: string]: string | number;
foo: any; // ok
foo2: string; // ok
foo3: number; // ok
foo4: boolean; // error
foo5: E; // ok - subtype of number
foo6: Date; // error
foo7: RegExp; // error
foo8: { bar: number }; // error
foo9: I8; // error
foo10: A; // error
foo11: A2<number>; // error
foo12: (x) => number; //error
foo13: <T>(x: T) => T; // error
foo14: typeof f; // error
foo15: typeof c; // error
foo16: T; // error
foo17: Object; // error
foo18: {}; // error
}
interface I2<T> {
[x: string]: E | number;
foo: any; // ok
foo2: string; // error
foo3: number; // ok
foo4: boolean; // error
foo5: E; // ok
foo6: Date; // error
foo7: RegExp; // error
foo8: { bar: number }; // error
foo9: I8; // error
foo10: A; // error
foo11: A2<number>; // error
foo12: (x) => number; //error
foo13: <T>(x: T) => T; // error
foo14: typeof f; // error
foo15: typeof c; // error
foo16: T; // error
foo17: Object; // error
foo18: {}; // error
}
//// [subtypesOfUnion.js]
var E;
(function (E) {
E[E["e1"] = 0] = "e1";
E[E["e2"] = 1] = "e2";
})(E || (E = {}));
var A = (function () {
function A() {
}
return A;
})();
var A2 = (function () {
function A2() {
}
return A2;
})();
function f() {
}
var f;
(function (f) {
f.bar = 1;
})(f || (f = {}));
var c = (function () {
function c() {
}
return c;
})();
var c;
(function (c) {
c.bar = 1;
})(c || (c = {}));

View file

@ -0,0 +1,52 @@
enum E { e1, e2 }
interface I8 { [x: string]: number[]; }
class A { foo: number; }
class A2<T> { foo: T; }
function f() { }
module f { export var bar = 1; }
class c { baz: string }
module c { export var bar = 1; }
// A type T is a subtype of a union type U if T is a subtype of any type in U.
interface I1<T> {
[x: string]: string | number;
foo: any; // ok
foo2: string; // ok
foo3: number; // ok
foo4: boolean; // error
foo5: E; // ok - subtype of number
foo6: Date; // error
foo7: RegExp; // error
foo8: { bar: number }; // error
foo9: I8; // error
foo10: A; // error
foo11: A2<number>; // error
foo12: (x) => number; //error
foo13: <T>(x: T) => T; // error
foo14: typeof f; // error
foo15: typeof c; // error
foo16: T; // error
foo17: Object; // error
foo18: {}; // error
}
interface I2<T> {
[x: string]: E | number;
foo: any; // ok
foo2: string; // error
foo3: number; // ok
foo4: boolean; // error
foo5: E; // ok
foo6: Date; // error
foo7: RegExp; // error
foo8: { bar: number }; // error
foo9: I8; // error
foo10: A; // error
foo11: A2<number>; // error
foo12: (x) => number; //error
foo13: <T>(x: T) => T; // error
foo14: typeof f; // error
foo15: typeof c; // error
foo16: T; // error
foo17: Object; // error
foo18: {}; // error
}