From 703cd5d4cfed329688984c1d26ae6fc844b6cdd3 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 3 Nov 2014 09:46:32 -0800 Subject: [PATCH] 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 --- .../reference/subtypesOfUnion.errors.txt | 142 ++++++++++++++++++ tests/baselines/reference/subtypesOfUnion.js | 85 +++++++++++ .../subtypesAndSuperTypes/subtypesOfUnion.ts | 52 +++++++ 3 files changed, 279 insertions(+) create mode 100644 tests/baselines/reference/subtypesOfUnion.errors.txt create mode 100644 tests/baselines/reference/subtypesOfUnion.js create mode 100644 tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts diff --git a/tests/baselines/reference/subtypesOfUnion.errors.txt b/tests/baselines/reference/subtypesOfUnion.errors.txt new file mode 100644 index 00000000000..fade5377814 --- /dev/null +++ b/tests/baselines/reference/subtypesOfUnion.errors.txt @@ -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' 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 '(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' 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 '(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 { 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 { + [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; // error + ~~~~~~~~~~~~~~~~~~ +!!! error TS2411: Property 'foo11' of type 'A2' 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: (x: T) => T; // error + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2411: Property 'foo13' of type '(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 { + [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; // error + ~~~~~~~~~~~~~~~~~~ +!!! error TS2411: Property 'foo11' of type 'A2' 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: (x: T) => T; // error + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2411: Property 'foo13' of type '(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'. + } \ No newline at end of file diff --git a/tests/baselines/reference/subtypesOfUnion.js b/tests/baselines/reference/subtypesOfUnion.js new file mode 100644 index 00000000000..a18ebb944b9 --- /dev/null +++ b/tests/baselines/reference/subtypesOfUnion.js @@ -0,0 +1,85 @@ +//// [subtypesOfUnion.ts] +enum E { e1, e2 } +interface I8 { [x: string]: number[]; } +class A { foo: number; } +class A2 { 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 { + [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; // error + foo12: (x) => number; //error + foo13: (x: T) => T; // error + foo14: typeof f; // error + foo15: typeof c; // error + foo16: T; // error + foo17: Object; // error + foo18: {}; // error +} +interface I2 { + [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; // error + foo12: (x) => number; //error + foo13: (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 = {})); diff --git a/tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts b/tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts new file mode 100644 index 00000000000..c20bafeeabf --- /dev/null +++ b/tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypesOfUnion.ts @@ -0,0 +1,52 @@ +enum E { e1, e2 } +interface I8 { [x: string]: number[]; } +class A { foo: number; } +class A2 { 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 { + [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; // error + foo12: (x) => number; //error + foo13: (x: T) => T; // error + foo14: typeof f; // error + foo15: typeof c; // error + foo16: T; // error + foo17: Object; // error + foo18: {}; // error +} +interface I2 { + [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; // error + foo12: (x) => number; //error + foo13: (x: T) => T; // error + foo14: typeof f; // error + foo15: typeof c; // error + foo16: T; // error + foo17: Object; // error + foo18: {}; // error +} \ No newline at end of file