mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-20 05:17:43 -05:00
Tests to check equivalency of union types
A union type encompasses an unordered set of unrelated types (that is, types that aren’t subtypes of each other). The following rules govern union types: • A | B is equivalent to A if B is a subtype of A. • A | B is equivalent to B | A. • AB | C is equivalent to A | BC, where AB is A | B and BC is B | C.
This commit is contained in:
54
tests/baselines/reference/unionTypeEquivalence.js
Normal file
54
tests/baselines/reference/unionTypeEquivalence.js
Normal file
@@ -0,0 +1,54 @@
|
||||
//// [unionTypeEquivalence.ts]
|
||||
// A | B is equivalent to A if B is a subtype of A
|
||||
class C { }
|
||||
class D extends C { }
|
||||
var x: C;
|
||||
var x : C | D;
|
||||
|
||||
// A | B is equivalent to B | A.
|
||||
var y: string | number;
|
||||
var y : number | string;
|
||||
|
||||
// AB | C is equivalent to A | BC, where AB is A | B and BC is B | C.
|
||||
var z : string | number | boolean;
|
||||
var z : (string | number) | boolean;
|
||||
var z : string | (number | boolean);
|
||||
var AB : string | number;
|
||||
var BC : number | boolean;
|
||||
var z1: typeof AB | boolean;
|
||||
var z1: string | typeof BC;
|
||||
|
||||
|
||||
//// [unionTypeEquivalence.js]
|
||||
var __extends = this.__extends || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
__.prototype = b.prototype;
|
||||
d.prototype = new __();
|
||||
};
|
||||
// A | B is equivalent to A if B is a subtype of A
|
||||
var C = (function () {
|
||||
function C() {
|
||||
}
|
||||
return C;
|
||||
})();
|
||||
var D = (function (_super) {
|
||||
__extends(D, _super);
|
||||
function D() {
|
||||
_super.apply(this, arguments);
|
||||
}
|
||||
return D;
|
||||
})(C);
|
||||
var x;
|
||||
var x;
|
||||
// A | B is equivalent to B | A.
|
||||
var y;
|
||||
var y;
|
||||
// AB | C is equivalent to A | BC, where AB is A | B and BC is B | C.
|
||||
var z;
|
||||
var z;
|
||||
var z;
|
||||
var AB;
|
||||
var BC;
|
||||
var z1;
|
||||
var z1;
|
||||
49
tests/baselines/reference/unionTypeEquivalence.types
Normal file
49
tests/baselines/reference/unionTypeEquivalence.types
Normal file
@@ -0,0 +1,49 @@
|
||||
=== tests/cases/conformance/types/union/unionTypeEquivalence.ts ===
|
||||
// A | B is equivalent to A if B is a subtype of A
|
||||
class C { }
|
||||
>C : C
|
||||
|
||||
class D extends C { }
|
||||
>D : D
|
||||
>C : C
|
||||
|
||||
var x: C;
|
||||
>x : C
|
||||
>C : C
|
||||
|
||||
var x : C | D;
|
||||
>x : C
|
||||
>C : C
|
||||
>D : D
|
||||
|
||||
// A | B is equivalent to B | A.
|
||||
var y: string | number;
|
||||
>y : string | number
|
||||
|
||||
var y : number | string;
|
||||
>y : string | number
|
||||
|
||||
// AB | C is equivalent to A | BC, where AB is A | B and BC is B | C.
|
||||
var z : string | number | boolean;
|
||||
>z : string | number | boolean
|
||||
|
||||
var z : (string | number) | boolean;
|
||||
>z : string | number | boolean
|
||||
|
||||
var z : string | (number | boolean);
|
||||
>z : string | number | boolean
|
||||
|
||||
var AB : string | number;
|
||||
>AB : string | number
|
||||
|
||||
var BC : number | boolean;
|
||||
>BC : number | boolean
|
||||
|
||||
var z1: typeof AB | boolean;
|
||||
>z1 : string | number | boolean
|
||||
>AB : string | number
|
||||
|
||||
var z1: string | typeof BC;
|
||||
>z1 : string | number | boolean
|
||||
>BC : number | boolean
|
||||
|
||||
18
tests/cases/conformance/types/union/unionTypeEquivalence.ts
Normal file
18
tests/cases/conformance/types/union/unionTypeEquivalence.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
// A | B is equivalent to A if B is a subtype of A
|
||||
class C { }
|
||||
class D extends C { }
|
||||
var x: C;
|
||||
var x : C | D;
|
||||
|
||||
// A | B is equivalent to B | A.
|
||||
var y: string | number;
|
||||
var y : number | string;
|
||||
|
||||
// AB | C is equivalent to A | BC, where AB is A | B and BC is B | C.
|
||||
var z : string | number | boolean;
|
||||
var z : (string | number) | boolean;
|
||||
var z : string | (number | boolean);
|
||||
var AB : string | number;
|
||||
var BC : number | boolean;
|
||||
var z1: typeof AB | boolean;
|
||||
var z1: string | typeof BC;
|
||||
Reference in New Issue
Block a user