TypeScript/tests/baselines/reference/discriminatedUnionTypes1.js
Yui cfc20a956a [Transforms] Merging Master to Transforms on 06/21 (#9294)
* Initial support for globs in tsconfig.json

* Added caching, more tests

* Added stubs to ChakraHost interface for readDirectoryNames/readFileNames

* Fixed typos in comments

* Changed name of 'reduce' method added to FileSet

* Heavily revised implementation that relies on an updated 'readDirectory' API.

* more tests

* Minor update to shims.ts for forthcoming VS support for globs.

* Detailed comments for regular expressions and renamed some files.

* Comment cleanup

* Fixed new linter warnings

* Add upper limit for the program size, fix readDirectory for the symlink files

* Add comments

* CR feedback / Change upper limit / Add disableSizeLimit compiler option

* online and offline CR feedback

* Don't count current opened client file if it's TS file

* Speed up file searching

* Make language service optional for a project

* Fix failed tests

* Fix project updateing issue after editing config file

* Fixing linter and test errors

* Bringing back excludes error and fixing faulty test

* Fixing lint errors

* Passing regular expressions to native hosts

* Fix merging issues and multiple project scenario

* Refactoring

* add test and spit commandLineParser changes to another PR

* Fix #8523

* check the declaration and use order if both are not in module file

* Type guards using discriminant properties of string literal types

* Fix #9098: report missing function impelementation errors for merged classes and namespaces

* Narrow type in case/default sections in switch on discriminant property

* No implicit returns following exhaustive switch statements

* Narrow non-union types to ensure consistent results

* Add tests

* No Need to store dot token when parsing property access expression

* Added tests.

* Accepted baselines.

* Check tuple types when getting the type node's type.

* Accepted baselines.

* Fix #9173: clear out lib and types before creating a program in transpileModule

* Added tests.

* Accepted baselines.

* Always check type assertion types.

* Clear out unused compiler options when transpiling

* Accepted baselines.

* improve error message for extending interface

* accept baselines

* Use helper functions to simplify range tests

* Remove String, Number, and Boolean from TypeFlags.Falsy

* Add regression test

* Accept new baselines

* Allow property declarations in .js files

* Remove old test

* Do not use Object.assing in test

* Fix comment

* Refactor code to make if statements cheaper

* ignore casing when converting a source file path to relative path

* add tests & add branches for module interface

* Using baselines for transpile unittests (#9195)

* Conver to Transpile unittest to use baselines instead

* Add baselines

* Fix linting error

* use resolveEntityName to find interface

* add new tests for extends interface

* address code style

* Refactor navigation bar

* routine dom update

* Updating readDirectory for tsserverProjectSystem unit tests

* Array#map -> ts.map.

* Responding to PR feedback

* Add conditional index signature for Canvas2DContextAttributes (https://github.com/Microsoft/TypeScript/issues/9244)

* Add libcheck tests

* Add missing worker types

* Accept webworker baselines

* Classify `this` in parameter position as a keyword

* Adding more matchFiles test cases

* Use implicit boolean casts; it doesn't hurt performance

* Use getCanonicalFileName

* export interface used by other exported functions

* Fix from merging with master

* Update tests and baselines from merging with master

* Remove using dotToken as it is no longer needed

* Update baselines from removing dotToken

* Address PR: Add NodeEmitFlags to no indent when emit

* Address PR; and refactor setting NodeEmitFlags for createMemberAccessForPropertyName

* Update baselines
2016-07-11 12:41:12 -07:00

253 lines
5.5 KiB
TypeScript

//// [discriminatedUnionTypes1.ts]
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
interface Circle {
kind: "circle";
radius: number;
}
type Shape = Square | Rectangle | Circle;
function area1(s: Shape) {
if (s.kind === "square") {
return s.size * s.size;
}
else if (s.kind === "circle") {
return Math.PI * s.radius * s.radius;
}
else if (s.kind === "rectangle") {
return s.width * s.height;
}
else {
return 0;
}
}
function area2(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
}
function assertNever(x: never): never {
throw new Error("Unexpected object: " + x);
}
function area3(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
default: return assertNever(s);
}
}
function area4(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
return assertNever(s);
}
type Message =
{ kind: "A", x: string } |
{ kind: "B" | "C", y: number } |
{ kind: "D" };
function f1(m: Message) {
if (m.kind === "A") {
m; // { kind: "A", x: string }
}
else if (m.kind === "D") {
m; // { kind: "D" }
}
else {
m; // { kind: "B" | "C", y: number }
}
}
function f2(m: Message) {
if (m.kind === "A") {
return;
}
m; // { kind: "B" | "C", y: number } | { kind: "D" }
}
function f3(m: Message) {
if (m.kind === "X") {
m; // never
}
}
function f4(m: Message, x: "A" | "D") {
if (m.kind == x) {
m; // { kind: "A", x: string } | { kind: "D" }
}
}
function f5(m: Message) {
switch (m.kind) {
case "A":
m; // { kind: "A", x: string }
break;
case "D":
m; // { kind: "D" }
break;
default:
m; // { kind: "B" | "C", y: number }
}
}
function f6(m: Message) {
switch (m.kind) {
case "A":
m; // { kind: "A", x: string }
case "D":
m; // { kind: "A", x: string } | { kind: "D" }
break;
default:
m; // { kind: "B" | "C", y: number }
}
}
function f7(m: Message) {
switch (m.kind) {
case "A":
case "B":
return;
}
m; // { kind: "B" | "C", y: number } | { kind: "D" }
}
function f8(m: Message) {
switch (m.kind) {
case "A":
return;
case "D":
throw new Error();
}
m; // { kind: "B" | "C", y: number }
}
//// [discriminatedUnionTypes1.js]
function area1(s) {
if (s.kind === "square") {
return s.size * s.size;
}
else if (s.kind === "circle") {
return Math.PI * s.radius * s.radius;
}
else if (s.kind === "rectangle") {
return s.width * s.height;
}
else {
return 0;
}
}
function area2(s) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
}
function assertNever(x) {
throw new Error("Unexpected object: " + x);
}
function area3(s) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
default: return assertNever(s);
}
}
function area4(s) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
return assertNever(s);
}
function f1(m) {
if (m.kind === "A") {
m; // { kind: "A", x: string }
}
else if (m.kind === "D") {
m; // { kind: "D" }
}
else {
m; // { kind: "B" | "C", y: number }
}
}
function f2(m) {
if (m.kind === "A") {
return;
}
m; // { kind: "B" | "C", y: number } | { kind: "D" }
}
function f3(m) {
if (m.kind === "X") {
m; // never
}
}
function f4(m, x) {
if (m.kind == x) {
m; // { kind: "A", x: string } | { kind: "D" }
}
}
function f5(m) {
switch (m.kind) {
case "A":
m; // { kind: "A", x: string }
break;
case "D":
m; // { kind: "D" }
break;
default:
m; // { kind: "B" | "C", y: number }
}
}
function f6(m) {
switch (m.kind) {
case "A":
m; // { kind: "A", x: string }
case "D":
m; // { kind: "A", x: string } | { kind: "D" }
break;
default:
m; // { kind: "B" | "C", y: number }
}
}
function f7(m) {
switch (m.kind) {
case "A":
case "B":
return;
}
m; // { kind: "B" | "C", y: number } | { kind: "D" }
}
function f8(m) {
switch (m.kind) {
case "A":
return;
case "D":
throw new Error();
}
m; // { kind: "B" | "C", y: number }
}