From 5cbf17d989d6f746de480631fa6b05d43d1afd8f Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 14 Dec 2015 14:16:31 -0800 Subject: [PATCH] Added tests. --- .../stringLiteralTypesAsTags02.ts | 42 +++++++++++++++++ .../stringLiteralTypesAsTags03.ts | 46 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags02.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags03.ts diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags02.ts new file mode 100644 index 00000000000..9fdc942a3f2 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags02.ts @@ -0,0 +1,42 @@ +// @declaration: true + +type Kind = "A" | "B" + +interface Entity { + kind: Kind; +} + +interface A extends Entity { + kind: "A"; + a: number; +} + +interface B extends Entity { + kind: "B"; + b: string; +} + +function hasKind(entity: Entity, kind: "A"): entity is A; +function hasKind(entity: Entity, kind: "B"): entity is B; +function hasKind(entity: Entity, kind: Kind): entity is (A | B) { + return entity.kind === kind; +} + +let x: A = { + kind: "A", + a: 100, +} + +if (hasKind(x, "A")) { + let a = x; +} +else { + let b = x; +} + +if (!hasKind(x, "B")) { + let c = x; +} +else { + let d = x; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags03.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags03.ts new file mode 100644 index 00000000000..96011d27255 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags03.ts @@ -0,0 +1,46 @@ +// @declaration: true + +type Kind = "A" | "B" + +interface Entity { + kind: Kind; +} + +interface A extends Entity { + kind: "A"; + a: number; +} + +interface B extends Entity { + kind: "B"; + b: string; +} + +// Currently (2015-12-14), we write '"A" | "A"' and '"B" | "B"' to avoid +// interpreting respective overloads as "specialized" signatures. +// That way, we can avoid the need to look for a compatible overload +// signature and simply check compatibility with the implementation. +function hasKind(entity: Entity, kind: "A" | "A"): entity is A; +function hasKind(entity: Entity, kind: "B" | "B"): entity is B; +function hasKind(entity: Entity, kind: Kind): entity is Entity { + return entity.kind === kind; +} + +let x: A = { + kind: "A", + a: 100, +} + +if (hasKind(x, "A")) { + let a = x; +} +else { + let b = x; +} + +if (!hasKind(x, "B")) { + let c = x; +} +else { + let d = x; +} \ No newline at end of file