diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags02.ts new file mode 100644 index 0000000000..9fdc942a3f --- /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 0000000000..96011d2725 --- /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