From 311a0cff55e5837f9972fb43d65e06768babccc5 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 10 Sep 2015 17:58:54 -0700 Subject: [PATCH 01/38] Added tests. --- .../stringLiteralTypesAsTags01.ts | 43 +++++++++++++++ .../stringLiteralTypesInUnionTypes01.ts | 21 ++++++++ .../stringLiteralTypesInUnionTypes02.ts | 21 ++++++++ .../stringLiteralTypesInUnionTypes03.ts | 21 ++++++++ .../stringLiteralTypesInUnionTypes04.ts | 38 +++++++++++++ ...ingLiteralTypesInVariableDeclarations01.ts | 19 +++++++ .../stringLiteralTypesOverloads01.ts | 53 +++++++++++++++++++ .../stringLiteralTypesOverloads02.ts | 51 ++++++++++++++++++ .../stringLiteralTypesTypePredicates01.ts | 25 +++++++++ 9 files changed, 292 insertions(+) create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts new file mode 100644 index 0000000000000..cea04f0818e75 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts @@ -0,0 +1,43 @@ +// @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 Entity; +function hasKind(entity: Entity, kind: Kind): boolean { + return kind === is; +} + +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/stringLiteralTypesInUnionTypes01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts new file mode 100644 index 0000000000000..b8ed1b47dd404 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts @@ -0,0 +1,21 @@ +// @declaration: true + +type T = "foo" | "bar" | "baz"; + +var x: "foo" | "bar" | "baz" = "foo"; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts new file mode 100644 index 0000000000000..2cc63ab44605d --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts @@ -0,0 +1,21 @@ +// @declaration: true + +type T = string | "foo" | "bar" | "baz"; + +var x: "foo" | "bar" | "baz" | string = "foo"; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts new file mode 100644 index 0000000000000..d5c6a38af799c --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts @@ -0,0 +1,21 @@ +// @declaration: true + +type T = number | "foo" | "bar"; + +var x: "foo" | "bar" | number; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts new file mode 100644 index 0000000000000..e9d062b0e5c20 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts @@ -0,0 +1,38 @@ +// @declaration: true + +type T = "" | "foo"; + +let x: T = ""; +let y: T = "foo"; + +if (x === "") { + let a = x; +} + +if (x !== "") { + let b = x; +} + +if (x == "") { + let c = x; +} + +if (x != "") { + let d = x; +} + +if (x) { + let e = x; +} + +if (!x) { + let f = x; +} + +if (!!x) { + let g = x; +} + +if (!!!x) { + let h = x; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts new file mode 100644 index 0000000000000..4f0063876876a --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts @@ -0,0 +1,19 @@ +// @declaration: true + +let a: ""; +var b: "foo"; +let c: "bar"; +const d: "baz"; + +a = ""; +b = "foo"; +c = "bar"; + +let e: "" = ""; +var f: "foo" = "foo"; +let g: "bar" = "bar"; +const h: "baz" = "baz"; + +e = ""; +f = "foo"; +g = "bar"; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts new file mode 100644 index 0000000000000..d88167eaeffe7 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts @@ -0,0 +1,53 @@ +// @declaration: true + +type PrimitiveName = 'string' | 'number' | 'boolean'; + +function getFalsyPrimitive(x: "string"): string; +function getFalsyPrimitive(x: "number"): number; +function getFalsyPrimitive(x: "boolean"): boolean; +function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +function getFalsyPrimitive(x: "number" | "string"): number | string; +function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +function getFalsyPrimitive(x: PrimitiveName) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; +} + +namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); +} + +const string: "string" = "string" +const number: "number" = "number" +const boolean: "boolean" = "boolean" + +const stringOrNumber = string || number; +const stringOrBoolean = string || boolean; +const booleanOrNumber = number || boolean; +const stringOrBooleanOrNumber = stringOrBoolean || number; + +namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); +} + + diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts new file mode 100644 index 0000000000000..5801aa50076b2 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts @@ -0,0 +1,51 @@ +// @declaration: true + +function getFalsyPrimitive(x: "string"): string; +function getFalsyPrimitive(x: "number"): number; +function getFalsyPrimitive(x: "boolean"): boolean; +function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +function getFalsyPrimitive(x: "number" | "string"): number | string; +function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +function getFalsyPrimitive(x: string) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; +} + +namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); +} + +const string: "string" = "string" +const number: "number" = "number" +const boolean: "boolean" = "boolean" + +const stringOrNumber = string || number; +const stringOrBoolean = string || boolean; +const booleanOrNumber = number || boolean; +const stringOrBooleanOrNumber = stringOrBoolean || number; + +namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); +} + + diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts new file mode 100644 index 0000000000000..a78918c8122be --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts @@ -0,0 +1,25 @@ +// @declaration: true + +type Kind = "A" | "B" + +function kindIs(kind: Kind, is: "A"): kind is "A"; +function kindIs(kind: Kind, is: "B"): kind is "B"; +function kindIs(kind: Kind, is: Kind): boolean { + return kind === is; +} + +var x: Kind = "A"; + +if (kindIs(x, "A")) { + let a = x; +} +else { + let b = x; +} + +if (!kindIs(x, "B")) { + let c = x; +} +else { + let d = x; +} \ No newline at end of file From 911e90730601f35a24dcd16d2e6d5f4fc6b12529 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 1 Oct 2015 15:26:48 -0700 Subject: [PATCH 02/38] Added test for string literal types in type arguments. --- .../typeArgumentsWithStringLiteralTypes01.ts | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts diff --git a/tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts b/tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts new file mode 100644 index 0000000000000..08c971458e354 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts @@ -0,0 +1,113 @@ +// @declaration: true + +declare function randBool(): boolean; +declare function takeReturnString(str: string): string; +declare function takeReturnHello(str: "Hello"): "Hello"; +declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; + +function fun1(x: T, y: T) { + return randBool() ? x : y; +} + +function fun2(x: T, y: U) { + return randBool() ? x : y; +} + +function fun3(...args: T[]): T { + return args[+randBool()]; +} + +namespace n1 { + // The following should all come back as strings. + // They should be assignable to/from something of a type 'string'. + // They should not be assignable to either "Hello" or "World". + export let a = fun1("Hello", "World"); + export let b = fun1("Hello", "Hello"); + export let c = fun2("Hello", "World"); + export let d = fun2("Hello", "Hello"); + export let e = fun3("Hello", "Hello", "World", "Foo"); + + // Should be valid + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Passing these as arguments should cause an error. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + +namespace n2 { + // The following (regardless of errors) should come back typed + // as "Hello" (or "Hello" | "Hello"). + export let a = fun1<"Hello">("Hello", "Hello"); + export let b = fun1<"Hello">("Hello", "World"); + export let c = fun2<"Hello", "Hello">("Hello", "Hello"); + export let d = fun2<"Hello", "Hello">("Hello", "World"); + export let e = fun3<"Hello">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Should be valid + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Assignment from the returned value should cause an error. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + + +namespace n3 { + // The following (regardless of errors) should come back typed + // as "Hello" | "World" (or "World" | "Hello"). + export let a = fun2<"Hello", "World">("Hello", "World"); + export let b = fun2<"Hello", "World">("World", "Hello"); + export let c = fun2<"World", "Hello">("Hello", "Hello"); + export let d = fun2<"World", "Hello">("World", "World"); + export let e = fun3<"Hello" | "World">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Both should be valid. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} \ No newline at end of file From f04cc39a68a7a576ca9e6b7149b076cd9eb95fd9 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 1 Oct 2015 15:54:16 -0700 Subject: [PATCH 03/38] Accepted baselines. --- .../stringLiteralTypesAsTags01.errors.txt | 86 +++++ .../reference/stringLiteralTypesAsTags01.js | 65 ++++ ...tringLiteralTypesInUnionTypes01.errors.txt | 59 ++++ .../stringLiteralTypesInUnionTypes01.js | 40 +++ ...tringLiteralTypesInUnionTypes02.errors.txt | 62 ++++ .../stringLiteralTypesInUnionTypes02.js | 40 +++ ...tringLiteralTypesInUnionTypes03.errors.txt | 50 +++ .../stringLiteralTypesInUnionTypes03.js | 39 +++ ...tringLiteralTypesInUnionTypes04.errors.txt | 52 +++ .../stringLiteralTypesInUnionTypes04.js | 67 ++++ ...alTypesInVariableDeclarations01.errors.txt | 81 +++++ ...ingLiteralTypesInVariableDeclarations01.js | 35 ++ .../stringLiteralTypesOverloads01.errors.txt | 146 ++++++++ .../stringLiteralTypesOverloads01.js | 93 +++++ .../stringLiteralTypesOverloads02.errors.txt | 129 +++++++ .../stringLiteralTypesOverloads02.js | 90 +++++ ...ingLiteralTypesTypePredicates01.errors.txt | 57 +++ .../stringLiteralTypesTypePredicates01.js | 46 +++ ...gumentsWithStringLiteralTypes01.errors.txt | 325 ++++++++++++++++++ .../typeArgumentsWithStringLiteralTypes01.js | 221 ++++++++++++ 20 files changed, 1783 insertions(+) create mode 100644 tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesAsTags01.js create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes01.js create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes02.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes02.js create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes03.js create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes04.js create mode 100644 tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js create mode 100644 tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesOverloads01.js create mode 100644 tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesOverloads02.js create mode 100644 tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesTypePredicates01.js create mode 100644 tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt create mode 100644 tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js diff --git a/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt new file mode 100644 index 0000000000000..292cc8ea5b7d2 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt @@ -0,0 +1,86 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,12): error TS4081: Exported type alias 'Kind' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,13): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,13): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,19): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(9,10): error TS4033: Property 'kind' of exported interface has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(9,11): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(14,10): error TS4033: Property 'kind' of exported interface has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(14,11): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(18,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(19,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(20,10): error TS2394: Overload signature is not compatible with function implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(22,21): error TS2304: Cannot find name 'is'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(25,5): error TS2322: Type '{ kind: string; a: number; }' is not assignable to type 'A'. + Property '"A"' is missing in type '{ kind: string; a: number; }'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (13 errors) ==== + + type Kind = "A" | "B" + +!!! error TS4081: Exported type alias 'Kind' has or is using private name ''. + ~~~ +!!! error TS1110: Type expected. + ~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + interface Entity { + kind: Kind; + } + + interface A extends Entity { + kind: "A"; + +!!! error TS4033: Property 'kind' of exported interface has or is using private name ''. + ~~~ +!!! error TS1110: Type expected. + a: number; + } + + interface B extends Entity { + kind: "B"; + +!!! error TS4033: Property 'kind' of exported interface has or is using private name ''. + ~~~ +!!! error TS1110: Type expected. + b: string; + } + + function hasKind(entity: Entity, kind: "A"): entity is A; + ~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function hasKind(entity: Entity, kind: "B"): entity is B; + ~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function hasKind(entity: Entity, kind: Kind): entity is Entity; + ~~~~~~~ +!!! error TS2394: Overload signature is not compatible with function implementation. + function hasKind(entity: Entity, kind: Kind): boolean { + return kind === is; + ~~ +!!! error TS2304: Cannot find name 'is'. + } + + let x: A = { + ~ +!!! error TS2322: Type '{ kind: string; a: number; }' is not assignable to type 'A'. +!!! error TS2322: Property '"A"' is missing in type '{ kind: string; a: number; }'. + 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/baselines/reference/stringLiteralTypesAsTags01.js b/tests/baselines/reference/stringLiteralTypesAsTags01.js new file mode 100644 index 0000000000000..2ce20607790a8 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.js @@ -0,0 +1,65 @@ +//// [stringLiteralTypesAsTags01.ts] + +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 Entity; +function hasKind(entity: Entity, kind: Kind): boolean { + return kind === is; +} + +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; +} + +//// [stringLiteralTypesAsTags01.js] +"A" | "B"; +function hasKind(entity, kind) { + return kind === is; +} +var x = { + kind: "A", + a: 100 +}; +if (hasKind(x, "A")) { + var a = x; +} +else { + var b = x; +} +if (!hasKind(x, "B")) { + var c = x; +} +else { + var d = x; +} diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt new file mode 100644 index 0000000000000..fcc13629ceb62 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt @@ -0,0 +1,59 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,9): error TS4081: Exported type alias 'T' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,10): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,10): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,18): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,26): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,7): error TS4025: Exported variable 'x' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,8): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,16): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,24): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,30): error TS1005: ',' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,32): error TS1134: Variable declaration expected. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts (12 errors) ==== + + type T = "foo" | "bar" | "baz"; + +!!! error TS4081: Exported type alias 'T' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + var x: "foo" | "bar" | "baz" = "foo"; + +!!! error TS4025: Exported variable 'x' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~ +!!! error TS1005: ',' expected. + ~~~~~ +!!! error TS1134: Variable declaration expected. + var y: T = "bar"; + + if (x === "foo") { + let a = x; + } + else if (x !== "bar") { + let b = x || y; + } + else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; + } + + x = y; + y = x; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js new file mode 100644 index 0000000000000..4eaaec42ceb75 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js @@ -0,0 +1,40 @@ +//// [stringLiteralTypesInUnionTypes01.ts] + +type T = "foo" | "bar" | "baz"; + +var x: "foo" | "bar" | "baz" = "foo"; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; + +//// [stringLiteralTypesInUnionTypes01.js] +"foo" | "bar" | "baz"; +var x = "foo" | "bar" | "baz"; +"foo"; +var y = "bar"; +if (x === "foo") { + var a = x; +} +else if (x !== "bar") { + var b = x || y; +} +else { + var c = x; + var d = y; + var e = c || d; +} +x = y; +y = x; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.errors.txt new file mode 100644 index 0000000000000..dc8523051aac4 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.errors.txt @@ -0,0 +1,62 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,18): error TS4081: Exported type alias 'T' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,19): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,19): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,27): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,35): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,7): error TS4025: Exported variable 'x' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,8): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,16): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,24): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,32): error TS2304: Cannot find name 'string'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,39): error TS1005: ',' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,41): error TS1134: Variable declaration expected. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts (13 errors) ==== + + type T = string | "foo" | "bar" | "baz"; + +!!! error TS4081: Exported type alias 'T' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + var x: "foo" | "bar" | "baz" | string = "foo"; + +!!! error TS4025: Exported variable 'x' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~~ +!!! error TS2304: Cannot find name 'string'. + ~ +!!! error TS1005: ',' expected. + ~~~~~ +!!! error TS1134: Variable declaration expected. + var y: T = "bar"; + + if (x === "foo") { + let a = x; + } + else if (x !== "bar") { + let b = x || y; + } + else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; + } + + x = y; + y = x; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js new file mode 100644 index 0000000000000..19b309980f87c --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js @@ -0,0 +1,40 @@ +//// [stringLiteralTypesInUnionTypes02.ts] + +type T = string | "foo" | "bar" | "baz"; + +var x: "foo" | "bar" | "baz" | string = "foo"; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; + +//// [stringLiteralTypesInUnionTypes02.js] +"foo" | "bar" | "baz"; +var x = "foo" | "bar" | "baz" | string; +"foo"; +var y = "bar"; +if (x === "foo") { + var a = x; +} +else if (x !== "bar") { + var b = x || y; +} +else { + var c = x; + var d = y; + var e = c || d; +} +x = y; +y = x; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt new file mode 100644 index 0000000000000..1595c62532272 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt @@ -0,0 +1,50 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(2,18): error TS4081: Exported type alias 'T' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(2,19): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(2,19): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(2,27): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,7): error TS4025: Exported variable 'x' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,8): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,16): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,24): error TS2304: Cannot find name 'number'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts (9 errors) ==== + + type T = number | "foo" | "bar"; + +!!! error TS4081: Exported type alias 'T' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + var x: "foo" | "bar" | number; + +!!! error TS4025: Exported variable 'x' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~~ +!!! error TS2304: Cannot find name 'number'. + var y: T = "bar"; + + if (x === "foo") { + let a = x; + } + else if (x !== "bar") { + let b = x || y; + } + else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; + } + + x = y; + y = x; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js new file mode 100644 index 0000000000000..7705848be2bee --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js @@ -0,0 +1,39 @@ +//// [stringLiteralTypesInUnionTypes03.ts] + +type T = number | "foo" | "bar"; + +var x: "foo" | "bar" | number; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; + +//// [stringLiteralTypesInUnionTypes03.js] +"foo" | "bar"; +var x = "foo" | "bar" | number; +var y = "bar"; +if (x === "foo") { + var a = x; +} +else if (x !== "bar") { + var b = x || y; +} +else { + var c = x; + var d = y; + var e = c || d; +} +x = y; +y = x; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt new file mode 100644 index 0000000000000..66d3e21523786 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt @@ -0,0 +1,52 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(2,9): error TS4081: Exported type alias 'T' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(2,10): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(2,10): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(2,15): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts (4 errors) ==== + + type T = "" | "foo"; + +!!! error TS4081: Exported type alias 'T' has or is using private name ''. + ~~ +!!! error TS1110: Type expected. + ~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + let x: T = ""; + let y: T = "foo"; + + if (x === "") { + let a = x; + } + + if (x !== "") { + let b = x; + } + + if (x == "") { + let c = x; + } + + if (x != "") { + let d = x; + } + + if (x) { + let e = x; + } + + if (!x) { + let f = x; + } + + if (!!x) { + let g = x; + } + + if (!!!x) { + let h = x; + } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js new file mode 100644 index 0000000000000..5817d16ec8fe9 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js @@ -0,0 +1,67 @@ +//// [stringLiteralTypesInUnionTypes04.ts] + +type T = "" | "foo"; + +let x: T = ""; +let y: T = "foo"; + +if (x === "") { + let a = x; +} + +if (x !== "") { + let b = x; +} + +if (x == "") { + let c = x; +} + +if (x != "") { + let d = x; +} + +if (x) { + let e = x; +} + +if (!x) { + let f = x; +} + +if (!!x) { + let g = x; +} + +if (!!!x) { + let h = x; +} + +//// [stringLiteralTypesInUnionTypes04.js] +"" | "foo"; +var x = ""; +var y = "foo"; +if (x === "") { + var a = x; +} +if (x !== "") { + var b = x; +} +if (x == "") { + var c = x; +} +if (x != "") { + var d = x; +} +if (x) { + var e = x; +} +if (!x) { + var f = x; +} +if (!!x) { + var g = x; +} +if (!!!x) { + var h = x; +} diff --git a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt new file mode 100644 index 0000000000000..14233db44cd4b --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt @@ -0,0 +1,81 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(2,7): error TS4025: Exported variable 'a' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(2,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(3,7): error TS4025: Exported variable 'b' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(3,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(4,7): error TS4025: Exported variable 'c' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(4,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(5,9): error TS4025: Exported variable 'd' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(5,10): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(11,7): error TS4025: Exported variable 'e' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(11,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(11,8): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(12,7): error TS4025: Exported variable 'f' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(12,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(12,8): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(13,7): error TS4025: Exported variable 'g' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(13,8): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(13,8): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(14,9): error TS4025: Exported variable 'h' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(14,10): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(14,10): error TS2364: Invalid left-hand side of assignment expression. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts (20 errors) ==== + + let a: ""; + +!!! error TS4025: Exported variable 'a' has or is using private name ''. + ~~ +!!! error TS1110: Type expected. + var b: "foo"; + +!!! error TS4025: Exported variable 'b' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + let c: "bar"; + +!!! error TS4025: Exported variable 'c' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + const d: "baz"; + +!!! error TS4025: Exported variable 'd' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + + a = ""; + b = "foo"; + c = "bar"; + + let e: "" = ""; + +!!! error TS4025: Exported variable 'e' has or is using private name ''. + ~~ +!!! error TS1110: Type expected. + ~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + var f: "foo" = "foo"; + +!!! error TS4025: Exported variable 'f' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + let g: "bar" = "bar"; + +!!! error TS4025: Exported variable 'g' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + const h: "baz" = "baz"; + +!!! error TS4025: Exported variable 'h' has or is using private name ''. + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + + e = ""; + f = "foo"; + g = "bar"; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js new file mode 100644 index 0000000000000..4aae2a2438a1c --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js @@ -0,0 +1,35 @@ +//// [stringLiteralTypesInVariableDeclarations01.ts] + +let a: ""; +var b: "foo"; +let c: "bar"; +const d: "baz"; + +a = ""; +b = "foo"; +c = "bar"; + +let e: "" = ""; +var f: "foo" = "foo"; +let g: "bar" = "bar"; +const h: "baz" = "baz"; + +e = ""; +f = "foo"; +g = "bar"; + +//// [stringLiteralTypesInVariableDeclarations01.js] +var a = ""; +var b = "foo"; +var c = "bar"; +var d = "baz"; +a = ""; +b = "foo"; +c = "bar"; +var e = "" = ""; +var f = "foo" = "foo"; +var g = "bar" = "bar"; +var h = "baz" = "baz"; +e = ""; +f = "foo"; +g = "bar"; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt new file mode 100644 index 0000000000000..e41de718dfaff --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt @@ -0,0 +1,146 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,21): error TS4081: Exported type alias 'PrimitiveName' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,22): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,22): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,33): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,44): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(4,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(5,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(6,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(7,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(7,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(7,41): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(8,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(8,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(8,41): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(9,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(9,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(9,40): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(10,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(10,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(10,40): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(11,10): error TS2354: No best common type exists among return expressions. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,14): error TS4025: Exported variable 'string' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,15): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,15): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,14): error TS4025: Exported variable 'number' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,15): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,15): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,15): error TS4025: Exported variable 'boolean' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,16): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,16): error TS2364: Invalid left-hand side of assignment expression. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts (30 errors) ==== + + type PrimitiveName = 'string' | 'number' | 'boolean'; + +!!! error TS4081: Exported type alias 'PrimitiveName' has or is using private name ''. + ~~~~~~~~ +!!! error TS1110: Type expected. + ~~~~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + function getFalsyPrimitive(x: "string"): string; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function getFalsyPrimitive(x: "number"): number; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function getFalsyPrimitive(x: "boolean"): boolean; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + function getFalsyPrimitive(x: "number" | "string"): number | string; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + function getFalsyPrimitive(x: PrimitiveName) { + ~~~~~~~~~~~~~~~~~ +!!! error TS2354: No best common type exists among return expressions. + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; + } + + namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); + } + + const string: "string" = "string" + +!!! error TS4025: Exported variable 'string' has or is using private name ''. + ~~~~~~~~ +!!! error TS1110: Type expected. + ~~~~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + const number: "number" = "number" + +!!! error TS4025: Exported variable 'number' has or is using private name ''. + ~~~~~~~~ +!!! error TS1110: Type expected. + ~~~~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + const boolean: "boolean" = "boolean" + +!!! error TS4025: Exported variable 'boolean' has or is using private name ''. + ~~~~~~~~~ +!!! error TS1110: Type expected. + ~~~~~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + + const stringOrNumber = string || number; + const stringOrBoolean = string || boolean; + const booleanOrNumber = number || boolean; + const stringOrBooleanOrNumber = stringOrBoolean || number; + + namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); + } + + + \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.js b/tests/baselines/reference/stringLiteralTypesOverloads01.js new file mode 100644 index 0000000000000..a001f02c9c9ff --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.js @@ -0,0 +1,93 @@ +//// [stringLiteralTypesOverloads01.ts] + +type PrimitiveName = 'string' | 'number' | 'boolean'; + +function getFalsyPrimitive(x: "string"): string; +function getFalsyPrimitive(x: "number"): number; +function getFalsyPrimitive(x: "boolean"): boolean; +function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +function getFalsyPrimitive(x: "number" | "string"): number | string; +function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +function getFalsyPrimitive(x: PrimitiveName) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; +} + +namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); +} + +const string: "string" = "string" +const number: "number" = "number" +const boolean: "boolean" = "boolean" + +const stringOrNumber = string || number; +const stringOrBoolean = string || boolean; +const booleanOrNumber = number || boolean; +const stringOrBooleanOrNumber = stringOrBoolean || number; + +namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); +} + + + + +//// [stringLiteralTypesOverloads01.js] +'string' | 'number' | 'boolean'; +function getFalsyPrimitive(x) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + // Should be unreachable. + throw "Invalid value"; +} +var Consts1; +(function (Consts1) { + var EMPTY_STRING = getFalsyPrimitive("string"); + var ZERO = getFalsyPrimitive('number'); + var FALSE = getFalsyPrimitive("boolean"); +})(Consts1 || (Consts1 = {})); +var string = "string" = "string"; +var number = "number" = "number"; +var boolean = "boolean" = "boolean"; +var stringOrNumber = string || number; +var stringOrBoolean = string || boolean; +var booleanOrNumber = number || boolean; +var stringOrBooleanOrNumber = stringOrBoolean || number; +var Consts2; +(function (Consts2) { + var EMPTY_STRING = getFalsyPrimitive(string); + var ZERO = getFalsyPrimitive(number); + var FALSE = getFalsyPrimitive(boolean); + var a = getFalsyPrimitive(stringOrNumber); + var b = getFalsyPrimitive(stringOrBoolean); + var c = getFalsyPrimitive(booleanOrNumber); + var d = getFalsyPrimitive(stringOrBooleanOrNumber); +})(Consts2 || (Consts2 = {})); diff --git a/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt new file mode 100644 index 0000000000000..ff1e212c07176 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt @@ -0,0 +1,129 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(2,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(3,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(4,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(5,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(5,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(5,41): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(6,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(6,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(6,41): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(7,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(7,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(7,40): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(8,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(8,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(8,40): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(9,10): error TS2354: No best common type exists among return expressions. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(30,14): error TS4025: Exported variable 'string' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(30,15): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(30,15): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(31,14): error TS4025: Exported variable 'number' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(31,15): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(31,15): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32,15): error TS4025: Exported variable 'boolean' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32,16): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32,16): error TS2364: Invalid left-hand side of assignment expression. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts (25 errors) ==== + + function getFalsyPrimitive(x: "string"): string; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function getFalsyPrimitive(x: "number"): number; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function getFalsyPrimitive(x: "boolean"): boolean; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + function getFalsyPrimitive(x: "number" | "string"): number | string; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; + ~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + function getFalsyPrimitive(x: string) { + ~~~~~~~~~~~~~~~~~ +!!! error TS2354: No best common type exists among return expressions. + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; + } + + namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); + } + + const string: "string" = "string" + +!!! error TS4025: Exported variable 'string' has or is using private name ''. + ~~~~~~~~ +!!! error TS1110: Type expected. + ~~~~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + const number: "number" = "number" + +!!! error TS4025: Exported variable 'number' has or is using private name ''. + ~~~~~~~~ +!!! error TS1110: Type expected. + ~~~~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + const boolean: "boolean" = "boolean" + +!!! error TS4025: Exported variable 'boolean' has or is using private name ''. + ~~~~~~~~~ +!!! error TS1110: Type expected. + ~~~~~~~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + + const stringOrNumber = string || number; + const stringOrBoolean = string || boolean; + const booleanOrNumber = number || boolean; + const stringOrBooleanOrNumber = stringOrBoolean || number; + + namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); + } + + + \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesOverloads02.js b/tests/baselines/reference/stringLiteralTypesOverloads02.js new file mode 100644 index 0000000000000..ae38acebb9513 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads02.js @@ -0,0 +1,90 @@ +//// [stringLiteralTypesOverloads02.ts] + +function getFalsyPrimitive(x: "string"): string; +function getFalsyPrimitive(x: "number"): number; +function getFalsyPrimitive(x: "boolean"): boolean; +function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +function getFalsyPrimitive(x: "number" | "string"): number | string; +function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +function getFalsyPrimitive(x: string) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; +} + +namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); +} + +const string: "string" = "string" +const number: "number" = "number" +const boolean: "boolean" = "boolean" + +const stringOrNumber = string || number; +const stringOrBoolean = string || boolean; +const booleanOrNumber = number || boolean; +const stringOrBooleanOrNumber = stringOrBoolean || number; + +namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); +} + + + + +//// [stringLiteralTypesOverloads02.js] +function getFalsyPrimitive(x) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + // Should be unreachable. + throw "Invalid value"; +} +var Consts1; +(function (Consts1) { + var EMPTY_STRING = getFalsyPrimitive("string"); + var ZERO = getFalsyPrimitive('number'); + var FALSE = getFalsyPrimitive("boolean"); +})(Consts1 || (Consts1 = {})); +var string = "string" = "string"; +var number = "number" = "number"; +var boolean = "boolean" = "boolean"; +var stringOrNumber = string || number; +var stringOrBoolean = string || boolean; +var booleanOrNumber = number || boolean; +var stringOrBooleanOrNumber = stringOrBoolean || number; +var Consts2; +(function (Consts2) { + var EMPTY_STRING = getFalsyPrimitive(string); + var ZERO = getFalsyPrimitive(number); + var FALSE = getFalsyPrimitive(boolean); + var a = getFalsyPrimitive(stringOrNumber); + var b = getFalsyPrimitive(stringOrBoolean); + var c = getFalsyPrimitive(booleanOrNumber); + var d = getFalsyPrimitive(stringOrBooleanOrNumber); +})(Consts2 || (Consts2 = {})); diff --git a/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt b/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt new file mode 100644 index 0000000000000..a4cc525cdd2b4 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt @@ -0,0 +1,57 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(2,12): error TS4081: Exported type alias 'Kind' has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(2,13): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(2,13): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(2,19): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,10): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,46): error TS4060: Return type of exported function has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,47): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,10): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,46): error TS4060: Return type of exported function has or is using private name ''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,47): error TS1110: Type expected. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts (10 errors) ==== + + type Kind = "A" | "B" + +!!! error TS4081: Exported type alias 'Kind' has or is using private name ''. + ~~~ +!!! error TS1110: Type expected. + ~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + function kindIs(kind: Kind, is: "A"): kind is "A"; + ~~~~~~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + +!!! error TS4060: Return type of exported function has or is using private name ''. + ~~~ +!!! error TS1110: Type expected. + function kindIs(kind: Kind, is: "B"): kind is "B"; + ~~~~~~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + +!!! error TS4060: Return type of exported function has or is using private name ''. + ~~~ +!!! error TS1110: Type expected. + function kindIs(kind: Kind, is: Kind): boolean { + return kind === is; + } + + var x: Kind = "A"; + + if (kindIs(x, "A")) { + let a = x; + } + else { + let b = x; + } + + if (!kindIs(x, "B")) { + let c = x; + } + else { + let d = x; + } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesTypePredicates01.js b/tests/baselines/reference/stringLiteralTypesTypePredicates01.js new file mode 100644 index 0000000000000..791caed9b31c2 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesTypePredicates01.js @@ -0,0 +1,46 @@ +//// [stringLiteralTypesTypePredicates01.ts] + +type Kind = "A" | "B" + +function kindIs(kind: Kind, is: "A"): kind is "A"; +function kindIs(kind: Kind, is: "B"): kind is "B"; +function kindIs(kind: Kind, is: Kind): boolean { + return kind === is; +} + +var x: Kind = "A"; + +if (kindIs(x, "A")) { + let a = x; +} +else { + let b = x; +} + +if (!kindIs(x, "B")) { + let c = x; +} +else { + let d = x; +} + +//// [stringLiteralTypesTypePredicates01.js] +"A" | "B"; +"A"; +"B"; +function kindIs(kind, is) { + return kind === is; +} +var x = "A"; +if (kindIs(x, "A")) { + var a = x; +} +else { + var b = x; +} +if (!kindIs(x, "B")) { + var c = x; +} +else { + var d = x; +} diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt new file mode 100644 index 0000000000000..626d0e3d6f488 --- /dev/null +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt @@ -0,0 +1,325 @@ +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(4,18): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(4,48): error TS4060: Return type of exported function has or is using private name ''. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(4,49): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,18): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,39): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,52): error TS1005: '=' expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,63): error TS4060: Return type of exported function has or is using private name ''. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,64): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,64): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,74): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(37,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(38,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(39,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(40,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(41,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(44,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(45,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(46,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(47,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(48,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(54,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(54,20): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,20): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(56,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(56,34): error TS1134: Variable declaration expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,34): error TS1134: Variable declaration expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,20): error TS2365: Operator '<' cannot be applied to types '(...args: T[]) => T' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,20): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(61,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(62,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(63,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(64,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(65,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(68,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(69,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(70,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(71,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(72,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(75,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(76,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(77,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(78,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(79,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(86,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(86,34): error TS1134: Variable declaration expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,34): error TS1134: Variable declaration expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,34): error TS1134: Variable declaration expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,34): error TS1134: Variable declaration expected. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(90,20): error TS2365: Operator '<' cannot be applied to types '(...args: T[]) => T' and 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(90,20): error TS2447: The '|' operator is not allowed for boolean types. Consider using '||' instead. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(93,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(94,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(95,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(96,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(97,26): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(100,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(101,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(102,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(103,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(104,25): error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(107,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(108,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(109,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(110,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(111,30): error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. + + +==== tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts (70 errors) ==== + + declare function randBool(): boolean; + declare function takeReturnString(str: string): string; + declare function takeReturnHello(str: "Hello"): "Hello"; + ~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + +!!! error TS4060: Return type of exported function has or is using private name ''. + ~~~~~~~ +!!! error TS1110: Type expected. + declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~ +!!! error TS1005: '=' expected. + +!!! error TS4060: Return type of exported function has or is using private name ''. + ~~~~~~~ +!!! error TS1110: Type expected. + ~~~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + ~~~~~~~ +!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + function fun1(x: T, y: T) { + return randBool() ? x : y; + } + + function fun2(x: T, y: U) { + return randBool() ? x : y; + } + + function fun3(...args: T[]): T { + return args[+randBool()]; + } + + namespace n1 { + // The following should all come back as strings. + // They should be assignable to/from something of a type 'string'. + // They should not be assignable to either "Hello" or "World". + export let a = fun1("Hello", "World"); + export let b = fun1("Hello", "Hello"); + export let c = fun2("Hello", "World"); + export let d = fun2("Hello", "Hello"); + export let e = fun3("Hello", "Hello", "World", "Foo"); + + // Should be valid + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + b = takeReturnHello(b); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + c = takeReturnHello(c); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + d = takeReturnHello(d); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + e = takeReturnHello(e); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + + // Passing these as arguments should cause an error. + a = takeReturnHelloWorld(a); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + b = takeReturnHelloWorld(b); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + c = takeReturnHelloWorld(c); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + d = takeReturnHelloWorld(d); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + e = takeReturnHelloWorld(e); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + } + + namespace n2 { + // The following (regardless of errors) should come back typed + // as "Hello" (or "Hello" | "Hello"). + export let a = fun1<"Hello">("Hello", "Hello"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. + export let b = fun1<"Hello">("Hello", "World"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. + export let c = fun2<"Hello", "Hello">("Hello", "Hello"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. + ~~~~~~~ +!!! error TS1134: Variable declaration expected. + export let d = fun2<"Hello", "Hello">("Hello", "World"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. + ~~~~~~~ +!!! error TS1134: Variable declaration expected. + export let e = fun3<"Hello">("Hello", "World"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(...args: T[]) => T' and 'string'. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + b = takeReturnString(b); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + c = takeReturnString(c); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + d = takeReturnString(d); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + e = takeReturnString(e); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + + // Should be valid + a = takeReturnHello(a); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + b = takeReturnHello(b); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + c = takeReturnHello(c); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + d = takeReturnHello(d); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + e = takeReturnHello(e); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + + // Assignment from the returned value should cause an error. + a = takeReturnHelloWorld(a); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + b = takeReturnHelloWorld(b); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + c = takeReturnHelloWorld(c); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + d = takeReturnHelloWorld(d); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + e = takeReturnHelloWorld(e); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + } + + + namespace n3 { + // The following (regardless of errors) should come back typed + // as "Hello" | "World" (or "World" | "Hello"). + export let a = fun2<"Hello", "World">("Hello", "World"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. + ~~~~~~~ +!!! error TS1134: Variable declaration expected. + export let b = fun2<"Hello", "World">("World", "Hello"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. + ~~~~~~~ +!!! error TS1134: Variable declaration expected. + export let c = fun2<"World", "Hello">("Hello", "Hello"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. + ~~~~~~~ +!!! error TS1134: Variable declaration expected. + export let d = fun2<"World", "Hello">("World", "World"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. + ~~~~~~~ +!!! error TS1134: Variable declaration expected. + export let e = fun3<"Hello" | "World">("Hello", "World"); + ~~~~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '(...args: T[]) => T' and 'string'. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2447: The '|' operator is not allowed for boolean types. Consider using '||' instead. + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + b = takeReturnString(b); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + c = takeReturnString(c); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + d = takeReturnString(d); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + e = takeReturnString(e); + ~ +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + b = takeReturnHello(b); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + c = takeReturnHello(c); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + d = takeReturnHello(d); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + e = takeReturnHello(e); + ~ +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. + + // Both should be valid. + a = takeReturnHelloWorld(a); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + b = takeReturnHelloWorld(b); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + c = takeReturnHelloWorld(c); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + d = takeReturnHelloWorld(d); + ~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. + e = takeReturnHelloWorld(e); + ~ +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. + } \ No newline at end of file diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js new file mode 100644 index 0000000000000..15c7cf5ef2d00 --- /dev/null +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js @@ -0,0 +1,221 @@ +//// [typeArgumentsWithStringLiteralTypes01.ts] + +declare function randBool(): boolean; +declare function takeReturnString(str: string): string; +declare function takeReturnHello(str: "Hello"): "Hello"; +declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; + +function fun1(x: T, y: T) { + return randBool() ? x : y; +} + +function fun2(x: T, y: U) { + return randBool() ? x : y; +} + +function fun3(...args: T[]): T { + return args[+randBool()]; +} + +namespace n1 { + // The following should all come back as strings. + // They should be assignable to/from something of a type 'string'. + // They should not be assignable to either "Hello" or "World". + export let a = fun1("Hello", "World"); + export let b = fun1("Hello", "Hello"); + export let c = fun2("Hello", "World"); + export let d = fun2("Hello", "Hello"); + export let e = fun3("Hello", "Hello", "World", "Foo"); + + // Should be valid + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Passing these as arguments should cause an error. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + +namespace n2 { + // The following (regardless of errors) should come back typed + // as "Hello" (or "Hello" | "Hello"). + export let a = fun1<"Hello">("Hello", "Hello"); + export let b = fun1<"Hello">("Hello", "World"); + export let c = fun2<"Hello", "Hello">("Hello", "Hello"); + export let d = fun2<"Hello", "Hello">("Hello", "World"); + export let e = fun3<"Hello">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Should be valid + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Assignment from the returned value should cause an error. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + + +namespace n3 { + // The following (regardless of errors) should come back typed + // as "Hello" | "World" (or "World" | "Hello"). + export let a = fun2<"Hello", "World">("Hello", "World"); + export let b = fun2<"Hello", "World">("World", "Hello"); + export let c = fun2<"World", "Hello">("Hello", "Hello"); + export let d = fun2<"World", "Hello">("World", "World"); + export let e = fun3<"Hello" | "World">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Both should be valid. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + +//// [typeArgumentsWithStringLiteralTypes01.js] +"Hello"; +"Hello" | "World"; +function fun1(x, y) { + return randBool() ? x : y; +} +function fun2(x, y) { + return randBool() ? x : y; +} +function fun3() { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i - 0] = arguments[_i]; + } + return args[+randBool()]; +} +var n1; +(function (n1) { + // The following should all come back as strings. + // They should be assignable to/from something of a type 'string'. + // They should not be assignable to either "Hello" or "World". + n1.a = fun1("Hello", "World"); + n1.b = fun1("Hello", "Hello"); + n1.c = fun2("Hello", "World"); + n1.d = fun2("Hello", "Hello"); + n1.e = fun3("Hello", "Hello", "World", "Foo"); + // Should be valid + n1.a = takeReturnString(n1.a); + n1.b = takeReturnString(n1.b); + n1.c = takeReturnString(n1.c); + n1.d = takeReturnString(n1.d); + n1.e = takeReturnString(n1.e); + // Passing these as arguments should cause an error. + n1.a = takeReturnHello(n1.a); + n1.b = takeReturnHello(n1.b); + n1.c = takeReturnHello(n1.c); + n1.d = takeReturnHello(n1.d); + n1.e = takeReturnHello(n1.e); + // Passing these as arguments should cause an error. + n1.a = takeReturnHelloWorld(n1.a); + n1.b = takeReturnHelloWorld(n1.b); + n1.c = takeReturnHelloWorld(n1.c); + n1.d = takeReturnHelloWorld(n1.d); + n1.e = takeReturnHelloWorld(n1.e); +})(n1 || (n1 = {})); +var n2; +(function (n2) { + // The following (regardless of errors) should come back typed + // as "Hello" (or "Hello" | "Hello"). + n2.a = fun1 < "Hello" > ("Hello", "Hello"); + n2.b = fun1 < "Hello" > ("Hello", "World"); + n2.c = fun2 < "Hello"; + "Hello" > ("Hello", "Hello"); + n2.d = fun2 < "Hello"; + "Hello" > ("Hello", "World"); + n2.e = fun3 < "Hello" > ("Hello", "World"); + // Assignment from the returned value should cause an error. + n2.a = takeReturnString(n2.a); + n2.b = takeReturnString(n2.b); + n2.c = takeReturnString(n2.c); + n2.d = takeReturnString(n2.d); + n2.e = takeReturnString(n2.e); + // Should be valid + n2.a = takeReturnHello(n2.a); + n2.b = takeReturnHello(n2.b); + n2.c = takeReturnHello(n2.c); + n2.d = takeReturnHello(n2.d); + n2.e = takeReturnHello(n2.e); + // Assignment from the returned value should cause an error. + n2.a = takeReturnHelloWorld(n2.a); + n2.b = takeReturnHelloWorld(n2.b); + n2.c = takeReturnHelloWorld(n2.c); + n2.d = takeReturnHelloWorld(n2.d); + n2.e = takeReturnHelloWorld(n2.e); +})(n2 || (n2 = {})); +var n3; +(function (n3) { + // The following (regardless of errors) should come back typed + // as "Hello" | "World" (or "World" | "Hello"). + n3.a = fun2 < "Hello"; + "World" > ("Hello", "World"); + n3.b = fun2 < "Hello"; + "World" > ("World", "Hello"); + n3.c = fun2 < "World"; + "Hello" > ("Hello", "Hello"); + n3.d = fun2 < "World"; + "Hello" > ("World", "World"); + n3.e = fun3 < "Hello" | "World" > ("Hello", "World"); + // Assignment from the returned value should cause an error. + n3.a = takeReturnString(n3.a); + n3.b = takeReturnString(n3.b); + n3.c = takeReturnString(n3.c); + n3.d = takeReturnString(n3.d); + n3.e = takeReturnString(n3.e); + // Passing these as arguments should cause an error. + n3.a = takeReturnHello(n3.a); + n3.b = takeReturnHello(n3.b); + n3.c = takeReturnHello(n3.c); + n3.d = takeReturnHello(n3.d); + n3.e = takeReturnHello(n3.e); + // Both should be valid. + n3.a = takeReturnHelloWorld(n3.a); + n3.b = takeReturnHelloWorld(n3.b); + n3.c = takeReturnHelloWorld(n3.c); + n3.d = takeReturnHelloWorld(n3.d); + n3.e = takeReturnHelloWorld(n3.e); +})(n3 || (n3 = {})); From 191be4f8fea8f176c7abffeeb1346f2bfe436fc8 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 1 Oct 2015 15:55:36 -0700 Subject: [PATCH 04/38] Make string literals valid constituent types nodes in the parser. --- src/compiler/parser.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 23fb846c0b43c..769415dd94b5a 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1969,9 +1969,7 @@ namespace ts { function parseParameterType(): TypeNode { if (parseOptional(SyntaxKind.ColonToken)) { - return token === SyntaxKind.StringLiteral - ? parseLiteralNode(/*internName*/ true) - : parseType(); + return parseType(); } return undefined; @@ -2359,6 +2357,8 @@ namespace ts { // If these are followed by a dot, then parse these out as a dotted type reference instead. let node = tryParse(parseKeywordAndNoDot); return node || parseTypeReferenceOrTypePredicate(); + case SyntaxKind.StringLiteral: + return parseLiteralNode(/*internName*/ true) case SyntaxKind.VoidKeyword: return parseTokenNode(); case SyntaxKind.TypeOfKeyword: From 84786d82c14e3fae0f86d22e6fc635ecbac9dab8 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 1 Oct 2015 16:33:08 -0700 Subject: [PATCH 05/38] Accepted baselines. --- ...overy_IncompleteMemberVariable1.errors.txt | 34 ----- ...ErrorRecovery_IncompleteMemberVariable1.js | 1 - ...Recovery_IncompleteMemberVariable1.symbols | 67 ++++++++++ ...orRecovery_IncompleteMemberVariable1.types | 78 ++++++++++++ .../reference/stringLiteralType.errors.txt | 12 -- .../baselines/reference/stringLiteralType.js | 2 +- .../reference/stringLiteralType.symbols | 16 +++ .../reference/stringLiteralType.types | 16 +++ .../stringLiteralTypesAsTags01.errors.txt | 32 +---- .../reference/stringLiteralTypesAsTags01.js | 20 ++- ...tringLiteralTypesInUnionTypes01.errors.txt | 48 ++----- .../stringLiteralTypesInUnionTypes01.js | 10 +- ...tringLiteralTypesInUnionTypes02.errors.txt | 62 --------- .../stringLiteralTypesInUnionTypes02.js | 10 +- .../stringLiteralTypesInUnionTypes02.symbols | 52 ++++++++ .../stringLiteralTypesInUnionTypes02.types | 62 +++++++++ ...tringLiteralTypesInUnionTypes03.errors.txt | 40 ++---- .../stringLiteralTypesInUnionTypes03.js | 9 +- ...tringLiteralTypesInUnionTypes04.errors.txt | 24 ++-- .../stringLiteralTypesInUnionTypes04.js | 7 +- ...alTypesInVariableDeclarations01.errors.txt | 97 ++++++-------- ...ingLiteralTypesInVariableDeclarations01.js | 27 ++-- .../stringLiteralTypesOverloads01.errors.txt | 98 ++------------- .../stringLiteralTypesOverloads01.js | 29 ++++- .../stringLiteralTypesOverloads02.errors.txt | 83 ++---------- .../stringLiteralTypesOverloads02.js | 27 +++- ...ingLiteralTypesTypePredicates01.errors.txt | 39 ++---- .../stringLiteralTypesTypePredicates01.js | 10 +- ...gumentsWithStringLiteralTypes01.errors.txt | 119 +++++++++--------- .../typeArgumentsWithStringLiteralTypes01.js | 33 ++++- 30 files changed, 609 insertions(+), 555 deletions(-) delete mode 100644 tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.errors.txt create mode 100644 tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols create mode 100644 tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types delete mode 100644 tests/baselines/reference/stringLiteralType.errors.txt create mode 100644 tests/baselines/reference/stringLiteralType.symbols create mode 100644 tests/baselines/reference/stringLiteralType.types delete mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes02.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes02.symbols create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes02.types diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.errors.txt b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.errors.txt deleted file mode 100644 index 707e68c2c4542..0000000000000 --- a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.errors.txt +++ /dev/null @@ -1,34 +0,0 @@ -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IncompleteMemberVariables/parserErrorRecovery_IncompleteMemberVariable1.ts(12,21): error TS1110: Type expected. - - -==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IncompleteMemberVariables/parserErrorRecovery_IncompleteMemberVariable1.ts (1 errors) ==== - // Interface - interface IPoint { - getDist(): number; - } - - // Module - module Shapes { - - // Class - export class Point implements IPoint { - - public con: "hello"; - ~~~~~~~ -!!! error TS1110: Type expected. - // Constructor - constructor (public x: number, public y: number) { } - - // Instance member - getDist() { return Math.sqrt(this.x * this.x + this.y * this.y); } - - // Static member - static origin = new Point(0, 0); - } - - } - - // Local variables - var p: IPoint = new Shapes.Point(3, 4); - var dist = p.getDist(); - \ No newline at end of file diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.js b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.js index ae2509c3f4929..8055cd9d4947b 100644 --- a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.js +++ b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.js @@ -38,7 +38,6 @@ var Shapes; function Point(x, y) { this.x = x; this.y = y; - this.con = "hello"; } // Instance member Point.prototype.getDist = function () { return Math.sqrt(this.x * this.x + this.y * this.y); }; diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols new file mode 100644 index 0000000000000..db9cefbf64291 --- /dev/null +++ b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols @@ -0,0 +1,67 @@ +=== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IncompleteMemberVariables/parserErrorRecovery_IncompleteMemberVariable1.ts === +// Interface +interface IPoint { +>IPoint : Symbol(IPoint, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 0, 0)) + + getDist(): number; +>getDist : Symbol(getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 1, 18)) +} + +// Module +module Shapes { +>Shapes : Symbol(Shapes, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 3, 1)) + + // Class + export class Point implements IPoint { +>Point : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>IPoint : Symbol(IPoint, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 0, 0)) + + public con: "hello"; +>con : Symbol(con, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 9, 42)) + + // Constructor + constructor (public x: number, public y: number) { } +>x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) + + // Instance member + getDist() { return Math.sqrt(this.x * this.x + this.y * this.y); } +>getDist : Symbol(getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 60)) +>Math.sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, 620, 27)) +>Math : Symbol(Math, Decl(lib.d.ts, 522, 1), Decl(lib.d.ts, 633, 11)) +>sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, 620, 27)) +>this.x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>this.x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>this.y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) +>this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) +>this.y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) +>this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) + + // Static member + static origin = new Point(0, 0); +>origin : Symbol(Point.origin, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 16, 74)) +>Point : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) + } + +} + +// Local variables +var p: IPoint = new Shapes.Point(3, 4); +>p : Symbol(p, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 25, 3)) +>IPoint : Symbol(IPoint, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 0, 0)) +>Shapes.Point : Symbol(Shapes.Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>Shapes : Symbol(Shapes, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 3, 1)) +>Point : Symbol(Shapes.Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) + +var dist = p.getDist(); +>dist : Symbol(dist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 26, 3)) +>p.getDist : Symbol(IPoint.getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 1, 18)) +>p : Symbol(p, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 25, 3)) +>getDist : Symbol(IPoint.getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 1, 18)) + diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types new file mode 100644 index 0000000000000..bb51151cfd71b --- /dev/null +++ b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types @@ -0,0 +1,78 @@ +=== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IncompleteMemberVariables/parserErrorRecovery_IncompleteMemberVariable1.ts === +// Interface +interface IPoint { +>IPoint : IPoint + + getDist(): number; +>getDist : () => number +} + +// Module +module Shapes { +>Shapes : typeof Shapes + + // Class + export class Point implements IPoint { +>Point : Point +>IPoint : IPoint + + public con: "hello"; +>con : "hello" + + // Constructor + constructor (public x: number, public y: number) { } +>x : number +>y : number + + // Instance member + getDist() { return Math.sqrt(this.x * this.x + this.y * this.y); } +>getDist : () => number +>Math.sqrt(this.x * this.x + this.y * this.y) : number +>Math.sqrt : (x: number) => number +>Math : Math +>sqrt : (x: number) => number +>this.x * this.x + this.y * this.y : number +>this.x * this.x : number +>this.x : number +>this : Point +>x : number +>this.x : number +>this : Point +>x : number +>this.y * this.y : number +>this.y : number +>this : Point +>y : number +>this.y : number +>this : Point +>y : number + + // Static member + static origin = new Point(0, 0); +>origin : Point +>new Point(0, 0) : Point +>Point : typeof Point +>0 : number +>0 : number + } + +} + +// Local variables +var p: IPoint = new Shapes.Point(3, 4); +>p : IPoint +>IPoint : IPoint +>new Shapes.Point(3, 4) : Shapes.Point +>Shapes.Point : typeof Shapes.Point +>Shapes : typeof Shapes +>Point : typeof Shapes.Point +>3 : number +>4 : number + +var dist = p.getDist(); +>dist : number +>p.getDist() : number +>p.getDist : () => number +>p : IPoint +>getDist : () => number + diff --git a/tests/baselines/reference/stringLiteralType.errors.txt b/tests/baselines/reference/stringLiteralType.errors.txt deleted file mode 100644 index 275db139cf7b3..0000000000000 --- a/tests/baselines/reference/stringLiteralType.errors.txt +++ /dev/null @@ -1,12 +0,0 @@ -tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts(1,8): error TS1110: Type expected. - - -==== tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts (1 errors) ==== - var x: 'hi'; - ~~~~ -!!! error TS1110: Type expected. - - function f(x: 'hi'); - function f(x: string); - function f(x: any) { - } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralType.js b/tests/baselines/reference/stringLiteralType.js index 35f35f1cbef78..a6926453c47b0 100644 --- a/tests/baselines/reference/stringLiteralType.js +++ b/tests/baselines/reference/stringLiteralType.js @@ -7,6 +7,6 @@ function f(x: any) { } //// [stringLiteralType.js] -var x = 'hi'; +var x; function f(x) { } diff --git a/tests/baselines/reference/stringLiteralType.symbols b/tests/baselines/reference/stringLiteralType.symbols new file mode 100644 index 0000000000000..687d305244218 --- /dev/null +++ b/tests/baselines/reference/stringLiteralType.symbols @@ -0,0 +1,16 @@ +=== tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts === +var x: 'hi'; +>x : Symbol(x, Decl(stringLiteralType.ts, 0, 3)) + +function f(x: 'hi'); +>f : Symbol(f, Decl(stringLiteralType.ts, 0, 12), Decl(stringLiteralType.ts, 2, 20), Decl(stringLiteralType.ts, 3, 22)) +>x : Symbol(x, Decl(stringLiteralType.ts, 2, 11)) + +function f(x: string); +>f : Symbol(f, Decl(stringLiteralType.ts, 0, 12), Decl(stringLiteralType.ts, 2, 20), Decl(stringLiteralType.ts, 3, 22)) +>x : Symbol(x, Decl(stringLiteralType.ts, 3, 11)) + +function f(x: any) { +>f : Symbol(f, Decl(stringLiteralType.ts, 0, 12), Decl(stringLiteralType.ts, 2, 20), Decl(stringLiteralType.ts, 3, 22)) +>x : Symbol(x, Decl(stringLiteralType.ts, 4, 11)) +} diff --git a/tests/baselines/reference/stringLiteralType.types b/tests/baselines/reference/stringLiteralType.types new file mode 100644 index 0000000000000..5d2e13c4ba5fa --- /dev/null +++ b/tests/baselines/reference/stringLiteralType.types @@ -0,0 +1,16 @@ +=== tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts === +var x: 'hi'; +>x : 'hi' + +function f(x: 'hi'); +>f : { (x: 'hi'): any; (x: string): any; } +>x : 'hi' + +function f(x: string); +>f : { (x: 'hi'): any; (x: string): any; } +>x : string + +function f(x: any) { +>f : { (x: 'hi'): any; (x: string): any; } +>x : any +} diff --git a/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt index 292cc8ea5b7d2..df05e25304fcb 100644 --- a/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt @@ -1,30 +1,15 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,12): error TS4081: Exported type alias 'Kind' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,13): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,13): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,19): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(9,10): error TS4033: Property 'kind' of exported interface has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(9,11): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(14,10): error TS4033: Property 'kind' of exported interface has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(14,11): error TS1110: Type expected. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(18,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(19,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(20,10): error TS2394: Overload signature is not compatible with function implementation. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(22,21): error TS2304: Cannot find name 'is'. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(25,5): error TS2322: Type '{ kind: string; a: number; }' is not assignable to type 'A'. - Property '"A"' is missing in type '{ kind: string; a: number; }'. + Types of property 'kind' are incompatible. + Type 'string' is not assignable to type '"A"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (13 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (5 errors) ==== type Kind = "A" | "B" - -!!! error TS4081: Exported type alias 'Kind' has or is using private name ''. - ~~~ -!!! error TS1110: Type expected. - ~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. interface Entity { kind: Kind; @@ -32,19 +17,11 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(25,5): interface A extends Entity { kind: "A"; - -!!! error TS4033: Property 'kind' of exported interface has or is using private name ''. - ~~~ -!!! error TS1110: Type expected. a: number; } interface B extends Entity { kind: "B"; - -!!! error TS4033: Property 'kind' of exported interface has or is using private name ''. - ~~~ -!!! error TS1110: Type expected. b: string; } @@ -66,7 +43,8 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(25,5): let x: A = { ~ !!! error TS2322: Type '{ kind: string; a: number; }' is not assignable to type 'A'. -!!! error TS2322: Property '"A"' is missing in type '{ kind: string; a: number; }'. +!!! error TS2322: Types of property 'kind' are incompatible. +!!! error TS2322: Type 'string' is not assignable to type '"A"'. kind: "A", a: 100, } diff --git a/tests/baselines/reference/stringLiteralTypesAsTags01.js b/tests/baselines/reference/stringLiteralTypesAsTags01.js index 2ce20607790a8..3fe33ace4870a 100644 --- a/tests/baselines/reference/stringLiteralTypesAsTags01.js +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.js @@ -43,7 +43,6 @@ else { } //// [stringLiteralTypesAsTags01.js] -"A" | "B"; function hasKind(entity, kind) { return kind === is; } @@ -63,3 +62,22 @@ if (!hasKind(x, "B")) { else { var d = x; } + + +//// [stringLiteralTypesAsTags01.d.ts] +declare type Kind = "A" | "B"; +interface Entity { + kind: Kind; +} +interface A extends Entity { + kind: "A"; + a: number; +} +interface B extends Entity { + kind: "B"; + b: string; +} +declare function hasKind(entity: Entity, kind: "A"): entity is A; +declare function hasKind(entity: Entity, kind: "B"): entity is B; +declare function hasKind(entity: Entity, kind: Kind): entity is Entity; +declare let x: A; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt index fcc13629ceb62..14d0a892f55d0 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt @@ -1,47 +1,21 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,9): error TS4081: Exported type alias 'T' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,10): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,10): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,18): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(2,26): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,7): error TS4025: Exported variable 'x' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,8): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,16): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,24): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,30): error TS1005: ',' expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,32): error TS1134: Variable declaration expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,5): error TS2322: Type 'string' is not assignable to type '"foo" | "bar" | "baz"'. + Type 'string' is not assignable to type '"baz"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(5,5): error TS2322: Type 'string' is not assignable to type '"foo" | "bar" | "baz"'. + Type 'string' is not assignable to type '"baz"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts (12 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts (2 errors) ==== type T = "foo" | "bar" | "baz"; - -!!! error TS4081: Exported type alias 'T' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. var x: "foo" | "bar" | "baz" = "foo"; - -!!! error TS4025: Exported variable 'x' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~ -!!! error TS1005: ',' expected. - ~~~~~ -!!! error TS1134: Variable declaration expected. + ~ +!!! error TS2322: Type 'string' is not assignable to type '"foo" | "bar" | "baz"'. +!!! error TS2322: Type 'string' is not assignable to type '"baz"'. var y: T = "bar"; + ~ +!!! error TS2322: Type 'string' is not assignable to type '"foo" | "bar" | "baz"'. +!!! error TS2322: Type 'string' is not assignable to type '"baz"'. if (x === "foo") { let a = x; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js index 4eaaec42ceb75..d970310d68e32 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js @@ -21,9 +21,7 @@ x = y; y = x; //// [stringLiteralTypesInUnionTypes01.js] -"foo" | "bar" | "baz"; -var x = "foo" | "bar" | "baz"; -"foo"; +var x = "foo"; var y = "bar"; if (x === "foo") { var a = x; @@ -38,3 +36,9 @@ else { } x = y; y = x; + + +//// [stringLiteralTypesInUnionTypes01.d.ts] +declare type T = "foo" | "bar" | "baz"; +declare var x: "foo" | "bar" | "baz"; +declare var y: T; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.errors.txt deleted file mode 100644 index dc8523051aac4..0000000000000 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.errors.txt +++ /dev/null @@ -1,62 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,18): error TS4081: Exported type alias 'T' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,19): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,19): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,27): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(2,35): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,7): error TS4025: Exported variable 'x' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,8): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,16): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,24): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,32): error TS2304: Cannot find name 'string'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,39): error TS1005: ',' expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts(4,41): error TS1134: Variable declaration expected. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts (13 errors) ==== - - type T = string | "foo" | "bar" | "baz"; - -!!! error TS4081: Exported type alias 'T' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - - var x: "foo" | "bar" | "baz" | string = "foo"; - -!!! error TS4025: Exported variable 'x' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~~ -!!! error TS2304: Cannot find name 'string'. - ~ -!!! error TS1005: ',' expected. - ~~~~~ -!!! error TS1134: Variable declaration expected. - var y: T = "bar"; - - if (x === "foo") { - let a = x; - } - else if (x !== "bar") { - let b = x || y; - } - else { - let c = x; - let d = y; - let e: (typeof x) | (typeof y) = c || d; - } - - x = y; - y = x; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js index 19b309980f87c..19b9c837c9da8 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js @@ -21,9 +21,7 @@ x = y; y = x; //// [stringLiteralTypesInUnionTypes02.js] -"foo" | "bar" | "baz"; -var x = "foo" | "bar" | "baz" | string; -"foo"; +var x = "foo"; var y = "bar"; if (x === "foo") { var a = x; @@ -38,3 +36,9 @@ else { } x = y; y = x; + + +//// [stringLiteralTypesInUnionTypes02.d.ts] +declare type T = string | "foo" | "bar" | "baz"; +declare var x: "foo" | "bar" | "baz" | string; +declare var y: T; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.symbols b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.symbols new file mode 100644 index 0000000000000..c9b31dc710a02 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.symbols @@ -0,0 +1,52 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts === + +type T = string | "foo" | "bar" | "baz"; +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes02.ts, 0, 0)) + +var x: "foo" | "bar" | "baz" | string = "foo"; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + +var y: T = "bar"; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes02.ts, 0, 0)) + +if (x === "foo") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + + let a = x; +>a : Symbol(a, Decl(stringLiteralTypesInUnionTypes02.ts, 7, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) +} +else if (x !== "bar") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + + let b = x || y; +>b : Symbol(b, Decl(stringLiteralTypesInUnionTypes02.ts, 10, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) +} +else { + let c = x; +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes02.ts, 13, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + + let d = y; +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes02.ts, 14, 7)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) + + let e: (typeof x) | (typeof y) = c || d; +>e : Symbol(e, Decl(stringLiteralTypesInUnionTypes02.ts, 15, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes02.ts, 13, 7)) +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes02.ts, 14, 7)) +} + +x = y; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) + +y = x; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types new file mode 100644 index 0000000000000..569edc7781593 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types @@ -0,0 +1,62 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts === + +type T = string | "foo" | "bar" | "baz"; +>T : string | "foo" | "bar" | "baz" + +var x: "foo" | "bar" | "baz" | string = "foo"; +>x : "foo" | "bar" | "baz" | string +>"foo" : string + +var y: T = "bar"; +>y : string | "foo" | "bar" | "baz" +>T : string | "foo" | "bar" | "baz" +>"bar" : string + +if (x === "foo") { +>x === "foo" : boolean +>x : "foo" | "bar" | "baz" | string +>"foo" : string + + let a = x; +>a : "foo" | "bar" | "baz" | string +>x : "foo" | "bar" | "baz" | string +} +else if (x !== "bar") { +>x !== "bar" : boolean +>x : "foo" | "bar" | "baz" | string +>"bar" : string + + let b = x || y; +>b : string +>x || y : string +>x : "foo" | "bar" | "baz" | string +>y : string | "foo" | "bar" | "baz" +} +else { + let c = x; +>c : "foo" | "bar" | "baz" | string +>x : "foo" | "bar" | "baz" | string + + let d = y; +>d : string | "foo" | "bar" | "baz" +>y : string | "foo" | "bar" | "baz" + + let e: (typeof x) | (typeof y) = c || d; +>e : "foo" | "bar" | "baz" | string +>x : "foo" | "bar" | "baz" | string +>y : string | "foo" | "bar" | "baz" +>c || d : string +>c : "foo" | "bar" | "baz" | string +>d : string | "foo" | "bar" | "baz" +} + +x = y; +>x = y : string | "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" | string +>y : string | "foo" | "bar" | "baz" + +y = x; +>y = x : "foo" | "bar" | "baz" | string +>y : string | "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" | string + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt index 1595c62532272..8ff377b2e728e 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt @@ -1,43 +1,27 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(2,18): error TS4081: Exported type alias 'T' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(2,19): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(2,19): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(2,27): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,7): error TS4025: Exported variable 'x' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,8): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,16): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(4,24): error TS2304: Cannot find name 'number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(5,5): error TS2322: Type 'string' is not assignable to type 'number | "foo" | "bar"'. + Type 'string' is not assignable to type '"bar"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(7,5): error TS2365: Operator '===' cannot be applied to types '"foo" | "bar" | number' and 'string'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(10,10): error TS2365: Operator '!==' cannot be applied to types '"foo" | "bar" | number' and 'string'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts (9 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts (3 errors) ==== type T = number | "foo" | "bar"; - -!!! error TS4081: Exported type alias 'T' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. var x: "foo" | "bar" | number; - -!!! error TS4025: Exported variable 'x' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~~ -!!! error TS2304: Cannot find name 'number'. var y: T = "bar"; + ~ +!!! error TS2322: Type 'string' is not assignable to type 'number | "foo" | "bar"'. +!!! error TS2322: Type 'string' is not assignable to type '"bar"'. if (x === "foo") { + ~~~~~~~~~~~ +!!! error TS2365: Operator '===' cannot be applied to types '"foo" | "bar" | number' and 'string'. let a = x; } else if (x !== "bar") { + ~~~~~~~~~~~ +!!! error TS2365: Operator '!==' cannot be applied to types '"foo" | "bar" | number' and 'string'. let b = x || y; } else { diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js index 7705848be2bee..f6728e0b2fb2f 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js @@ -21,8 +21,7 @@ x = y; y = x; //// [stringLiteralTypesInUnionTypes03.js] -"foo" | "bar"; -var x = "foo" | "bar" | number; +var x; var y = "bar"; if (x === "foo") { var a = x; @@ -37,3 +36,9 @@ else { } x = y; y = x; + + +//// [stringLiteralTypesInUnionTypes03.d.ts] +declare type T = number | "foo" | "bar"; +declare var x: "foo" | "bar" | number; +declare var y: T; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt index 66d3e21523786..77cb9c09531a7 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt @@ -1,23 +1,21 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(2,9): error TS4081: Exported type alias 'T' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(2,10): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(2,10): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(2,15): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(4,5): error TS2322: Type 'string' is not assignable to type '"" | "foo"'. + Type 'string' is not assignable to type '"foo"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(5,5): error TS2322: Type 'string' is not assignable to type '"" | "foo"'. + Type 'string' is not assignable to type '"foo"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts (4 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts (2 errors) ==== type T = "" | "foo"; - -!!! error TS4081: Exported type alias 'T' has or is using private name ''. - ~~ -!!! error TS1110: Type expected. - ~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. let x: T = ""; + ~ +!!! error TS2322: Type 'string' is not assignable to type '"" | "foo"'. +!!! error TS2322: Type 'string' is not assignable to type '"foo"'. let y: T = "foo"; + ~ +!!! error TS2322: Type 'string' is not assignable to type '"" | "foo"'. +!!! error TS2322: Type 'string' is not assignable to type '"foo"'. if (x === "") { let a = x; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js index 5817d16ec8fe9..dc6cf51ad1e1c 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js @@ -38,7 +38,6 @@ if (!!!x) { } //// [stringLiteralTypesInUnionTypes04.js] -"" | "foo"; var x = ""; var y = "foo"; if (x === "") { @@ -65,3 +64,9 @@ if (!!x) { if (!!!x) { var h = x; } + + +//// [stringLiteralTypesInUnionTypes04.d.ts] +declare type T = "" | "foo"; +declare let x: T; +declare let y: T; diff --git a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt index 14233db44cd4b..3dd2adbbce08b 100644 --- a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt @@ -1,81 +1,54 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(2,7): error TS4025: Exported variable 'a' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(2,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(3,7): error TS4025: Exported variable 'b' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(3,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(4,7): error TS4025: Exported variable 'c' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(4,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(5,9): error TS4025: Exported variable 'd' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(5,10): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(11,7): error TS4025: Exported variable 'e' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(11,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(11,8): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(12,7): error TS4025: Exported variable 'f' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(12,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(12,8): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(13,7): error TS4025: Exported variable 'g' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(13,8): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(13,8): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(14,9): error TS4025: Exported variable 'h' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(14,10): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(14,10): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(5,7): error TS1155: 'const' declarations must be initialized +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(7,1): error TS2322: Type 'string' is not assignable to type '""'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(8,1): error TS2322: Type 'string' is not assignable to type '"foo"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(9,1): error TS2322: Type 'string' is not assignable to type '"bar"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(11,5): error TS2322: Type 'string' is not assignable to type '""'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(12,5): error TS2322: Type 'string' is not assignable to type '"foo"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(13,5): error TS2322: Type 'string' is not assignable to type '"bar"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(14,7): error TS2322: Type 'string' is not assignable to type '"baz"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(16,1): error TS2322: Type 'string' is not assignable to type '""'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(17,1): error TS2322: Type 'string' is not assignable to type '"foo"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(18,1): error TS2322: Type 'string' is not assignable to type '"bar"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts (20 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts (11 errors) ==== let a: ""; - -!!! error TS4025: Exported variable 'a' has or is using private name ''. - ~~ -!!! error TS1110: Type expected. var b: "foo"; - -!!! error TS4025: Exported variable 'b' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. let c: "bar"; - -!!! error TS4025: Exported variable 'c' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. const d: "baz"; - -!!! error TS4025: Exported variable 'd' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. + ~ +!!! error TS1155: 'const' declarations must be initialized a = ""; + ~ +!!! error TS2322: Type 'string' is not assignable to type '""'. b = "foo"; + ~ +!!! error TS2322: Type 'string' is not assignable to type '"foo"'. c = "bar"; + ~ +!!! error TS2322: Type 'string' is not assignable to type '"bar"'. let e: "" = ""; - -!!! error TS4025: Exported variable 'e' has or is using private name ''. - ~~ -!!! error TS1110: Type expected. - ~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~ +!!! error TS2322: Type 'string' is not assignable to type '""'. var f: "foo" = "foo"; - -!!! error TS4025: Exported variable 'f' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~ +!!! error TS2322: Type 'string' is not assignable to type '"foo"'. let g: "bar" = "bar"; - -!!! error TS4025: Exported variable 'g' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~ +!!! error TS2322: Type 'string' is not assignable to type '"bar"'. const h: "baz" = "baz"; - -!!! error TS4025: Exported variable 'h' has or is using private name ''. - ~~~~~ -!!! error TS1110: Type expected. - ~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~ +!!! error TS2322: Type 'string' is not assignable to type '"baz"'. e = ""; + ~ +!!! error TS2322: Type 'string' is not assignable to type '""'. f = "foo"; - g = "bar"; \ No newline at end of file + ~ +!!! error TS2322: Type 'string' is not assignable to type '"foo"'. + g = "bar"; + ~ +!!! error TS2322: Type 'string' is not assignable to type '"bar"'. \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js index 4aae2a2438a1c..15a4ca5d580db 100644 --- a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js +++ b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js @@ -19,17 +19,28 @@ f = "foo"; g = "bar"; //// [stringLiteralTypesInVariableDeclarations01.js] -var a = ""; -var b = "foo"; -var c = "bar"; -var d = "baz"; +var a; +var b; +var c; +var d; a = ""; b = "foo"; c = "bar"; -var e = "" = ""; -var f = "foo" = "foo"; -var g = "bar" = "bar"; -var h = "baz" = "baz"; +var e = ""; +var f = "foo"; +var g = "bar"; +var h = "baz"; e = ""; f = "foo"; g = "bar"; + + +//// [stringLiteralTypesInVariableDeclarations01.d.ts] +declare let a: ""; +declare var b: "foo"; +declare let c: "bar"; +declare const d: "baz"; +declare let e: ""; +declare var f: "foo"; +declare let g: "bar"; +declare const h: "baz"; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt index e41de718dfaff..378857dc2771f 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt @@ -1,86 +1,20 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,21): error TS4081: Exported type alias 'PrimitiveName' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,22): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,22): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,33): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(2,44): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(4,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(5,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(6,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(7,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(7,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(7,41): error TS1005: '=' expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(8,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(8,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(8,41): error TS1005: '=' expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(9,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(9,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(9,40): error TS1005: '=' expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(10,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(10,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(10,40): error TS1005: '=' expected. tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(11,10): error TS2354: No best common type exists among return expressions. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,14): error TS4025: Exported variable 'string' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,15): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,15): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,14): error TS4025: Exported variable 'number' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,15): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,15): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,15): error TS4025: Exported variable 'boolean' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,16): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,16): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,7): error TS2322: Type 'string' is not assignable to type '"string"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,7): error TS2322: Type 'string' is not assignable to type ''number''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,7): error TS2322: Type 'string' is not assignable to type '"boolean"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts (30 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts (4 errors) ==== type PrimitiveName = 'string' | 'number' | 'boolean'; - -!!! error TS4081: Exported type alias 'PrimitiveName' has or is using private name ''. - ~~~~~~~~ -!!! error TS1110: Type expected. - ~~~~~~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. function getFalsyPrimitive(x: "string"): string; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. function getFalsyPrimitive(x: "number"): number; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. function getFalsyPrimitive(x: "boolean"): boolean; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. function getFalsyPrimitive(x: "number" | "string"): number | string; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. function getFalsyPrimitive(x: PrimitiveName) { ~~~~~~~~~~~~~~~~~ !!! error TS2354: No best common type exists among return expressions. @@ -105,26 +39,14 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34, } const string: "string" = "string" - -!!! error TS4025: Exported variable 'string' has or is using private name ''. - ~~~~~~~~ -!!! error TS1110: Type expected. - ~~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"string"'. const number: "number" = "number" - -!!! error TS4025: Exported variable 'number' has or is using private name ''. - ~~~~~~~~ -!!! error TS1110: Type expected. - ~~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type ''number''. const boolean: "boolean" = "boolean" - -!!! error TS4025: Exported variable 'boolean' has or is using private name ''. - ~~~~~~~~~ -!!! error TS1110: Type expected. - ~~~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"boolean"'. const stringOrNumber = string || number; const stringOrBoolean = string || boolean; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.js b/tests/baselines/reference/stringLiteralTypesOverloads01.js index a001f02c9c9ff..23483776a9b88 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads01.js +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.js @@ -54,7 +54,6 @@ namespace Consts2 { //// [stringLiteralTypesOverloads01.js] -'string' | 'number' | 'boolean'; function getFalsyPrimitive(x) { if (x === "string") { return ""; @@ -74,9 +73,9 @@ var Consts1; var ZERO = getFalsyPrimitive('number'); var FALSE = getFalsyPrimitive("boolean"); })(Consts1 || (Consts1 = {})); -var string = "string" = "string"; -var number = "number" = "number"; -var boolean = "boolean" = "boolean"; +var string = "string"; +var number = "number"; +var boolean = "boolean"; var stringOrNumber = string || number; var stringOrBoolean = string || boolean; var booleanOrNumber = number || boolean; @@ -91,3 +90,25 @@ var Consts2; var c = getFalsyPrimitive(booleanOrNumber); var d = getFalsyPrimitive(stringOrBooleanOrNumber); })(Consts2 || (Consts2 = {})); + + +//// [stringLiteralTypesOverloads01.d.ts] +declare type PrimitiveName = 'string' | 'number' | 'boolean'; +declare function getFalsyPrimitive(x: "string"): string; +declare function getFalsyPrimitive(x: "number"): number; +declare function getFalsyPrimitive(x: "boolean"): boolean; +declare function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +declare function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +declare function getFalsyPrimitive(x: "number" | "string"): number | string; +declare function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +declare namespace Consts1 { +} +declare const string: "string"; +declare const number: "number"; +declare const boolean: "boolean"; +declare const stringOrNumber: "string" | 'number'; +declare const stringOrBoolean: "string" | "boolean"; +declare const booleanOrNumber: 'number' | "boolean"; +declare const stringOrBooleanOrNumber: "string" | "boolean" | 'number'; +declare namespace Consts2 { +} diff --git a/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt index ff1e212c07176..ae9fb9ce4f67f 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt @@ -1,69 +1,18 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(2,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(3,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(4,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(5,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(5,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(5,41): error TS1005: '=' expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(6,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(6,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(6,41): error TS1005: '=' expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(7,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(7,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(7,40): error TS1005: '=' expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(8,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(8,28): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(8,40): error TS1005: '=' expected. tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(9,10): error TS2354: No best common type exists among return expressions. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(30,14): error TS4025: Exported variable 'string' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(30,15): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(30,15): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(31,14): error TS4025: Exported variable 'number' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(31,15): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(31,15): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32,15): error TS4025: Exported variable 'boolean' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32,16): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32,16): error TS2364: Invalid left-hand side of assignment expression. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(30,7): error TS2322: Type 'string' is not assignable to type '"string"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(31,7): error TS2322: Type 'string' is not assignable to type '"number"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32,7): error TS2322: Type 'string' is not assignable to type '"boolean"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts (25 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts (4 errors) ==== function getFalsyPrimitive(x: "string"): string; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. function getFalsyPrimitive(x: "number"): number; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. function getFalsyPrimitive(x: "boolean"): boolean; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. function getFalsyPrimitive(x: "number" | "string"): number | string; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; - ~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. function getFalsyPrimitive(x: string) { ~~~~~~~~~~~~~~~~~ !!! error TS2354: No best common type exists among return expressions. @@ -88,26 +37,14 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32, } const string: "string" = "string" - -!!! error TS4025: Exported variable 'string' has or is using private name ''. - ~~~~~~~~ -!!! error TS1110: Type expected. - ~~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"string"'. const number: "number" = "number" - -!!! error TS4025: Exported variable 'number' has or is using private name ''. - ~~~~~~~~ -!!! error TS1110: Type expected. - ~~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"number"'. const boolean: "boolean" = "boolean" - -!!! error TS4025: Exported variable 'boolean' has or is using private name ''. - ~~~~~~~~~ -!!! error TS1110: Type expected. - ~~~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. + ~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"boolean"'. const stringOrNumber = string || number; const stringOrBoolean = string || boolean; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads02.js b/tests/baselines/reference/stringLiteralTypesOverloads02.js index ae38acebb9513..3c53f9380a88b 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads02.js +++ b/tests/baselines/reference/stringLiteralTypesOverloads02.js @@ -71,9 +71,9 @@ var Consts1; var ZERO = getFalsyPrimitive('number'); var FALSE = getFalsyPrimitive("boolean"); })(Consts1 || (Consts1 = {})); -var string = "string" = "string"; -var number = "number" = "number"; -var boolean = "boolean" = "boolean"; +var string = "string"; +var number = "number"; +var boolean = "boolean"; var stringOrNumber = string || number; var stringOrBoolean = string || boolean; var booleanOrNumber = number || boolean; @@ -88,3 +88,24 @@ var Consts2; var c = getFalsyPrimitive(booleanOrNumber); var d = getFalsyPrimitive(stringOrBooleanOrNumber); })(Consts2 || (Consts2 = {})); + + +//// [stringLiteralTypesOverloads02.d.ts] +declare function getFalsyPrimitive(x: "string"): string; +declare function getFalsyPrimitive(x: "number"): number; +declare function getFalsyPrimitive(x: "boolean"): boolean; +declare function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +declare function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +declare function getFalsyPrimitive(x: "number" | "string"): number | string; +declare function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +declare namespace Consts1 { +} +declare const string: "string"; +declare const number: "number"; +declare const boolean: "boolean"; +declare const stringOrNumber: "string" | "number"; +declare const stringOrBoolean: "string" | "boolean"; +declare const booleanOrNumber: "number" | "boolean"; +declare const stringOrBooleanOrNumber: "string" | "boolean" | "number"; +declare namespace Consts2 { +} diff --git a/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt b/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt index a4cc525cdd2b4..3e412a6c770f0 100644 --- a/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt @@ -1,46 +1,27 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(2,12): error TS4081: Exported type alias 'Kind' has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(2,13): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(2,13): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(2,19): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,10): error TS2391: Function implementation is missing or not immediately following the declaration. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,46): error TS4060: Return type of exported function has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,47): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,10): error TS2391: Function implementation is missing or not immediately following the declaration. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,46): error TS4060: Return type of exported function has or is using private name ''. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,47): error TS1110: Type expected. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(10,5): error TS2322: Type 'string' is not assignable to type '"A" | "B"'. + Type 'string' is not assignable to type '"B"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts (10 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts (3 errors) ==== type Kind = "A" | "B" - -!!! error TS4081: Exported type alias 'Kind' has or is using private name ''. - ~~~ -!!! error TS1110: Type expected. - ~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. function kindIs(kind: Kind, is: "A"): kind is "A"; ~~~~~~ -!!! error TS2391: Function implementation is missing or not immediately following the declaration. - -!!! error TS4060: Return type of exported function has or is using private name ''. - ~~~ -!!! error TS1110: Type expected. +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. function kindIs(kind: Kind, is: "B"): kind is "B"; ~~~~~~ -!!! error TS2391: Function implementation is missing or not immediately following the declaration. - -!!! error TS4060: Return type of exported function has or is using private name ''. - ~~~ -!!! error TS1110: Type expected. +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. function kindIs(kind: Kind, is: Kind): boolean { return kind === is; } var x: Kind = "A"; + ~ +!!! error TS2322: Type 'string' is not assignable to type '"A" | "B"'. +!!! error TS2322: Type 'string' is not assignable to type '"B"'. if (kindIs(x, "A")) { let a = x; diff --git a/tests/baselines/reference/stringLiteralTypesTypePredicates01.js b/tests/baselines/reference/stringLiteralTypesTypePredicates01.js index 791caed9b31c2..fd4129d272d05 100644 --- a/tests/baselines/reference/stringLiteralTypesTypePredicates01.js +++ b/tests/baselines/reference/stringLiteralTypesTypePredicates01.js @@ -25,9 +25,6 @@ else { } //// [stringLiteralTypesTypePredicates01.js] -"A" | "B"; -"A"; -"B"; function kindIs(kind, is) { return kind === is; } @@ -44,3 +41,10 @@ if (!kindIs(x, "B")) { else { var d = x; } + + +//// [stringLiteralTypesTypePredicates01.d.ts] +declare type Kind = "A" | "B"; +declare function kindIs(kind: Kind, is: "A"): kind is "A"; +declare function kindIs(kind: Kind, is: "B"): kind is "B"; +declare var x: Kind; diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt index 626d0e3d6f488..f6421445ae11f 100644 --- a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt @@ -1,23 +1,19 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(4,18): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(4,48): error TS4060: Return type of exported function has or is using private name ''. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(4,49): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,18): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,39): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,52): error TS1005: '=' expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,63): error TS4060: Return type of exported function has or is using private name ''. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,64): error TS1110: Type expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,64): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(5,74): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(37,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(38,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(39,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(40,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(41,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(44,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(45,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(46,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(47,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(48,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(44,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(45,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(46,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(47,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(48,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(54,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(54,20): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. @@ -38,11 +34,16 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(70,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(71,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(72,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(75,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(76,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(77,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(78,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(79,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(75,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(76,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(77,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(78,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(79,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(86,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(86,34): error TS1134: Variable declaration expected. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. @@ -63,39 +64,26 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(102,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(103,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(104,25): error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(107,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(108,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(109,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(110,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(111,30): error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(107,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(108,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(109,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(110,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. + Type 'boolean' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(111,30): error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello" | "World"'. + Type 'number' is not assignable to type '"World"'. -==== tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts (70 errors) ==== +==== tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts (61 errors) ==== declare function randBool(): boolean; declare function takeReturnString(str: string): string; declare function takeReturnHello(str: "Hello"): "Hello"; ~~~~~~~~~~~~~~~ !!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - -!!! error TS4060: Return type of exported function has or is using private name ''. - ~~~~~~~ -!!! error TS1110: Type expected. declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. - ~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~ -!!! error TS1005: '=' expected. - -!!! error TS4060: Return type of exported function has or is using private name ''. - ~~~~~~~ -!!! error TS1110: Type expected. - ~~~~~~~ -!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - ~~~~~~~ -!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. function fun1(x: T, y: T) { return randBool() ? x : y; @@ -146,19 +134,24 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 // Passing these as arguments should cause an error. a = takeReturnHelloWorld(a); ~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. b = takeReturnHelloWorld(b); ~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. c = takeReturnHelloWorld(c); ~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. d = takeReturnHelloWorld(d); ~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. e = takeReturnHelloWorld(e); ~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. } namespace n2 { @@ -227,19 +220,24 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 // Assignment from the returned value should cause an error. a = takeReturnHelloWorld(a); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. b = takeReturnHelloWorld(b); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. c = takeReturnHelloWorld(c); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. d = takeReturnHelloWorld(d); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. e = takeReturnHelloWorld(e); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. } @@ -309,17 +307,22 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 // Both should be valid. a = takeReturnHelloWorld(a); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. b = takeReturnHelloWorld(b); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. c = takeReturnHelloWorld(c); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. d = takeReturnHelloWorld(d); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. e = takeReturnHelloWorld(e); ~ -!!! error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'number' is not assignable to type '"World"'. } \ No newline at end of file diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js index 15c7cf5ef2d00..e6e2b695a3bae 100644 --- a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js @@ -113,8 +113,6 @@ namespace n3 { } //// [typeArgumentsWithStringLiteralTypes01.js] -"Hello"; -"Hello" | "World"; function fun1(x, y) { return randBool() ? x : y; } @@ -219,3 +217,34 @@ var n3; n3.d = takeReturnHelloWorld(n3.d); n3.e = takeReturnHelloWorld(n3.e); })(n3 || (n3 = {})); + + +//// [typeArgumentsWithStringLiteralTypes01.d.ts] +declare function randBool(): boolean; +declare function takeReturnString(str: string): string; +declare function takeReturnHello(str: "Hello"): "Hello"; +declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; +declare function fun1(x: T, y: T): T; +declare function fun2(x: T, y: U): T | U; +declare function fun3(...args: T[]): T; +declare namespace n1 { + let a: string; + let b: string; + let c: string; + let d: string; + let e: string; +} +declare namespace n2 { + let a: boolean; + let b: boolean; + let c: boolean; + let d: boolean; + let e: boolean; +} +declare namespace n3 { + let a: boolean; + let b: boolean; + let c: boolean; + let d: boolean; + let e: number; +} From dc0e368f824001d16bc7b22c7d0b8ac44305d924 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 1 Oct 2015 16:45:48 -0700 Subject: [PATCH 06/38] Make string literals valid types in type lists. --- src/compiler/parser.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 769415dd94b5a..a9d7f94715b08 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2387,6 +2387,7 @@ namespace ts { case SyntaxKind.OpenBracketToken: case SyntaxKind.LessThanToken: case SyntaxKind.NewKeyword: + case SyntaxKind.StringLiteral: return true; case SyntaxKind.OpenParenToken: // Only consider '(' the start of a type if followed by ')', '...', an identifier, a modifier, @@ -5446,6 +5447,7 @@ namespace ts { return parseTokenNode(); } + // TODO (drosen): Parse string literal types in JSDoc as well. return parseJSDocTypeReference(); } From 8891fba1498df42360b22faf5ac71753dcd4e0d2 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 1 Oct 2015 16:47:31 -0700 Subject: [PATCH 07/38] Accepted baselines. --- ...gumentsWithStringLiteralTypes01.errors.txt | 222 +++++------------- .../typeArgumentsWithStringLiteralTypes01.js | 46 ++-- .../typeParameterConstraints1.errors.txt | 5 +- 3 files changed, 74 insertions(+), 199 deletions(-) diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt index f6421445ae11f..413d21270039a 100644 --- a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt @@ -14,69 +14,29 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 Type 'string' is not assignable to type '"World"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(48,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. Type 'string' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(54,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(54,20): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,20): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(56,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(56,34): error TS1134: Variable declaration expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,34): error TS1134: Variable declaration expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,20): error TS2365: Operator '<' cannot be applied to types '(...args: T[]) => T' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,20): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(61,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(62,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(63,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(64,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(65,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(68,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(69,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(70,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(71,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(72,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(75,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(76,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(77,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(78,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(79,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(86,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(86,34): error TS1134: Variable declaration expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,34): error TS1134: Variable declaration expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,34): error TS1134: Variable declaration expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,20): error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,34): error TS1134: Variable declaration expected. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(90,20): error TS2365: Operator '<' cannot be applied to types '(...args: T[]) => T' and 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(90,20): error TS2447: The '|' operator is not allowed for boolean types. Consider using '||' instead. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(93,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(94,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(95,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(96,26): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(97,26): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(100,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(101,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(102,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(103,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(104,25): error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(107,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(108,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(109,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(110,30): error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. - Type 'boolean' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(111,30): error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello" | "World"'. - Type 'number' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,34): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,34): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(61,5): error TS2322: Type 'string' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(63,5): error TS2322: Type 'string' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(75,5): error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. + Type '"World"' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(77,5): error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. + Type '"World"' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(93,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(97,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(100,25): error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'. + Type '"World"' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(104,25): error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'. + Type '"World"' is not assignable to type '"Hello"'. -==== tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts (61 errors) ==== +==== tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts (25 errors) ==== declare function randBool(): boolean; declare function takeReturnString(str: string): string; @@ -158,86 +118,47 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 // The following (regardless of errors) should come back typed // as "Hello" (or "Hello" | "Hello"). export let a = fun1<"Hello">("Hello", "Hello"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. export let b = fun1<"Hello">("Hello", "World"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: T) => T' and 'string'. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. - export let c = fun2<"Hello", "Hello">("Hello", "Hello"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. ~~~~~~~ -!!! error TS1134: Variable declaration expected. +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + export let c = fun2<"Hello", "Hello">("Hello", "Hello"); export let d = fun2<"Hello", "Hello">("Hello", "World"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. - ~~~~~~~ -!!! error TS1134: Variable declaration expected. + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. export let e = fun3<"Hello">("Hello", "World"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(...args: T[]) => T' and 'string'. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'. + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. // Assignment from the returned value should cause an error. a = takeReturnString(a); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + ~ +!!! error TS2322: Type 'string' is not assignable to type '"Hello"'. b = takeReturnString(b); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. c = takeReturnString(c); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + ~ +!!! error TS2322: Type 'string' is not assignable to type '"Hello"'. d = takeReturnString(d); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. e = takeReturnString(e); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. // Should be valid a = takeReturnHello(a); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. b = takeReturnHello(b); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. c = takeReturnHello(c); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. d = takeReturnHello(d); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. e = takeReturnHello(e); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. // Assignment from the returned value should cause an error. a = takeReturnHelloWorld(a); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. + ~ +!!! error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. +!!! error TS2322: Type '"World"' is not assignable to type '"Hello"'. b = takeReturnHelloWorld(b); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. c = takeReturnHelloWorld(c); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. + ~ +!!! error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. +!!! error TS2322: Type '"World"' is not assignable to type '"Hello"'. d = takeReturnHelloWorld(d); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. e = takeReturnHelloWorld(e); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. } @@ -245,84 +166,47 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 // The following (regardless of errors) should come back typed // as "Hello" | "World" (or "World" | "Hello"). export let a = fun2<"Hello", "World">("Hello", "World"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. - ~~~~~~~ -!!! error TS1134: Variable declaration expected. export let b = fun2<"Hello", "World">("World", "Hello"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. - ~~~~~~~ -!!! error TS1134: Variable declaration expected. + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. export let c = fun2<"World", "Hello">("Hello", "Hello"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. - ~~~~~~~ -!!! error TS1134: Variable declaration expected. + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. export let d = fun2<"World", "Hello">("World", "World"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(x: T, y: U) => T | U' and 'string'. - ~~~~~~~ -!!! error TS1134: Variable declaration expected. + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. export let e = fun3<"Hello" | "World">("Hello", "World"); - ~~~~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '(...args: T[]) => T' and 'string'. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2447: The '|' operator is not allowed for boolean types. Consider using '||' instead. // Assignment from the returned value should cause an error. a = takeReturnString(a); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. + ~ +!!! error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. +!!! error TS2322: Type 'string' is not assignable to type '"World"'. b = takeReturnString(b); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. c = takeReturnString(c); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. d = takeReturnString(d); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'. e = takeReturnString(e); - ~ -!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. + ~ +!!! error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. +!!! error TS2322: Type 'string' is not assignable to type '"World"'. // Passing these as arguments should cause an error. a = takeReturnHello(a); ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Type '"World"' is not assignable to type '"Hello"'. b = takeReturnHello(b); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. c = takeReturnHello(c); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. d = takeReturnHello(d); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello"'. e = takeReturnHello(e); ~ -!!! error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Type '"World"' is not assignable to type '"Hello"'. // Both should be valid. a = takeReturnHelloWorld(a); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. b = takeReturnHelloWorld(b); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. c = takeReturnHelloWorld(c); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. d = takeReturnHelloWorld(d); - ~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'boolean' is not assignable to type '"World"'. e = takeReturnHelloWorld(e); - ~ -!!! error TS2345: Argument of type 'number' is not assignable to parameter of type '"Hello" | "World"'. -!!! error TS2345: Type 'number' is not assignable to type '"World"'. } \ No newline at end of file diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js index e6e2b695a3bae..c07dd22539648 100644 --- a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js @@ -159,13 +159,11 @@ var n2; (function (n2) { // The following (regardless of errors) should come back typed // as "Hello" (or "Hello" | "Hello"). - n2.a = fun1 < "Hello" > ("Hello", "Hello"); - n2.b = fun1 < "Hello" > ("Hello", "World"); - n2.c = fun2 < "Hello"; - "Hello" > ("Hello", "Hello"); - n2.d = fun2 < "Hello"; - "Hello" > ("Hello", "World"); - n2.e = fun3 < "Hello" > ("Hello", "World"); + n2.a = fun1("Hello", "Hello"); + n2.b = fun1("Hello", "World"); + n2.c = fun2("Hello", "Hello"); + n2.d = fun2("Hello", "World"); + n2.e = fun3("Hello", "World"); // Assignment from the returned value should cause an error. n2.a = takeReturnString(n2.a); n2.b = takeReturnString(n2.b); @@ -189,15 +187,11 @@ var n3; (function (n3) { // The following (regardless of errors) should come back typed // as "Hello" | "World" (or "World" | "Hello"). - n3.a = fun2 < "Hello"; - "World" > ("Hello", "World"); - n3.b = fun2 < "Hello"; - "World" > ("World", "Hello"); - n3.c = fun2 < "World"; - "Hello" > ("Hello", "Hello"); - n3.d = fun2 < "World"; - "Hello" > ("World", "World"); - n3.e = fun3 < "Hello" | "World" > ("Hello", "World"); + n3.a = fun2("Hello", "World"); + n3.b = fun2("World", "Hello"); + n3.c = fun2("Hello", "Hello"); + n3.d = fun2("World", "World"); + n3.e = fun3("Hello", "World"); // Assignment from the returned value should cause an error. n3.a = takeReturnString(n3.a); n3.b = takeReturnString(n3.b); @@ -235,16 +229,16 @@ declare namespace n1 { let e: string; } declare namespace n2 { - let a: boolean; - let b: boolean; - let c: boolean; - let d: boolean; - let e: boolean; + let a: "Hello"; + let b: any; + let c: "Hello"; + let d: any; + let e: any; } declare namespace n3 { - let a: boolean; - let b: boolean; - let c: boolean; - let d: boolean; - let e: number; + let a: "Hello" | "World"; + let b: any; + let c: any; + let d: any; + let e: "Hello" | "World"; } diff --git a/tests/baselines/reference/typeParameterConstraints1.errors.txt b/tests/baselines/reference/typeParameterConstraints1.errors.txt index f30fd4f6700b4..e837e19317deb 100644 --- a/tests/baselines/reference/typeParameterConstraints1.errors.txt +++ b/tests/baselines/reference/typeParameterConstraints1.errors.txt @@ -1,12 +1,11 @@ tests/cases/compiler/typeParameterConstraints1.ts(6,25): error TS2304: Cannot find name 'hm'. -tests/cases/compiler/typeParameterConstraints1.ts(8,25): error TS1110: Type expected. tests/cases/compiler/typeParameterConstraints1.ts(9,25): error TS1110: Type expected. tests/cases/compiler/typeParameterConstraints1.ts(10,26): error TS1110: Type expected. tests/cases/compiler/typeParameterConstraints1.ts(11,26): error TS1110: Type expected. tests/cases/compiler/typeParameterConstraints1.ts(12,26): error TS2304: Cannot find name 'undefined'. -==== tests/cases/compiler/typeParameterConstraints1.ts (6 errors) ==== +==== tests/cases/compiler/typeParameterConstraints1.ts (5 errors) ==== function foo1(test: T) { } function foo2(test: T) { } function foo3(test: T) { } @@ -17,8 +16,6 @@ tests/cases/compiler/typeParameterConstraints1.ts(12,26): error TS2304: Cannot f !!! error TS2304: Cannot find name 'hm'. function foo7(test: T) { } // valid function foo8(test: T) { } - ~~ -!!! error TS1110: Type expected. function foo9 (test: T) { } ~ !!! error TS1110: Type expected. From 82545ce8544cf0e24b278fa11c8afc8116de1c3c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 1 Oct 2015 16:58:03 -0700 Subject: [PATCH 08/38] Added test for string types in tuples. --- .../stringLiteralTypesAndTuples01.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts new file mode 100644 index 0000000000000..7ac9aa3b42742 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts @@ -0,0 +1,20 @@ +// @declaration: true + +// Should all be strings. +let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; + +type RexOrRaptor = "t-rex" | "raptor" +let [im, a, dinosaur]: ["I'm", "a", Dinosaur] = ['I\'m', 'a', 't-rex']; + +rawr(dinosaur); + +function rawr(dino: RexOrRaptor) { + if (dino === "t-rex") { + return "ROAAAAR!"; + } + if (dino === "raptor") { + return "yip yip!"; + } + + throw "Unexpected " + dino; +} \ No newline at end of file From ed927d8cf642065806f28bdc715f44cfc704872e Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 1 Oct 2015 16:58:17 -0700 Subject: [PATCH 09/38] Accepted baselines. --- .../stringLiteralTypesAndTuples01.errors.txt | 32 ++++++++++++++ .../stringLiteralTypesAndTuples01.js | 42 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesAndTuples01.js diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt b/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt new file mode 100644 index 0000000000000..849f4922e45fc --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt @@ -0,0 +1,32 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts(6,5): error TS2322: Type '[string, string, string]' is not assignable to type '["I'm", "a", any]'. + Types of property '0' are incompatible. + Type 'string' is not assignable to type '"I'm"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts(6,37): error TS2304: Cannot find name 'Dinosaur'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts (2 errors) ==== + + // Should all be strings. + let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; + + type RexOrRaptor = "t-rex" | "raptor" + let [im, a, dinosaur]: ["I'm", "a", Dinosaur] = ['I\'m', 'a', 't-rex']; + ~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '[string, string, string]' is not assignable to type '["I'm", "a", any]'. +!!! error TS2322: Types of property '0' are incompatible. +!!! error TS2322: Type 'string' is not assignable to type '"I'm"'. + ~~~~~~~~ +!!! error TS2304: Cannot find name 'Dinosaur'. + + rawr(dinosaur); + + function rawr(dino: RexOrRaptor) { + if (dino === "t-rex") { + return "ROAAAAR!"; + } + if (dino === "raptor") { + return "yip yip!"; + } + + throw "Unexpected " + dino; + } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.js b/tests/baselines/reference/stringLiteralTypesAndTuples01.js new file mode 100644 index 0000000000000..66e57b1eea5ac --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.js @@ -0,0 +1,42 @@ +//// [stringLiteralTypesAndTuples01.ts] + +// Should all be strings. +let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; + +type RexOrRaptor = "t-rex" | "raptor" +let [im, a, dinosaur]: ["I'm", "a", Dinosaur] = ['I\'m', 'a', 't-rex']; + +rawr(dinosaur); + +function rawr(dino: RexOrRaptor) { + if (dino === "t-rex") { + return "ROAAAAR!"; + } + if (dino === "raptor") { + return "yip yip!"; + } + + throw "Unexpected " + dino; +} + +//// [stringLiteralTypesAndTuples01.js] +// Should all be strings. +var _a = ["Hello", "Brave", "New", "World"], hello = _a[0], brave = _a[1], newish = _a[2], world = _a[3]; +var _b = ['I\'m', 'a', 't-rex'], im = _b[0], a = _b[1], dinosaur = _b[2]; +rawr(dinosaur); +function rawr(dino) { + if (dino === "t-rex") { + return "ROAAAAR!"; + } + if (dino === "raptor") { + return "yip yip!"; + } + throw "Unexpected " + dino; +} + + +//// [stringLiteralTypesAndTuples01.d.ts] +declare let hello: string, brave: string, newish: string, world: string; +declare type RexOrRaptor = "t-rex" | "raptor"; +declare let im: "I'm", a: "a", dinosaur: any; +declare function rawr(dino: RexOrRaptor): string; From a3e7ccb108b8897f969499941403e5803cef4e75 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 15:00:46 -0700 Subject: [PATCH 10/38] Use normalized text for text on string literal types. --- src/compiler/checker.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9f1bf504a0e92..c062398c1a95a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1620,7 +1620,7 @@ namespace ts { writeAnonymousType(type, flags); } else if (type.flags & TypeFlags.StringLiteral) { - writer.writeStringLiteral((type).text); + writer.writeStringLiteral(`"${escapeString((type).text)}"`); } else { // Should never get here @@ -4213,12 +4213,13 @@ namespace ts { } function getStringLiteralType(node: StringLiteral): StringLiteralType { - if (hasProperty(stringLiteralTypes, node.text)) { - return stringLiteralTypes[node.text]; + const text = node.text; + if (hasProperty(stringLiteralTypes, text)) { + return stringLiteralTypes[text]; } - let type = stringLiteralTypes[node.text] = createType(TypeFlags.StringLiteral); - type.text = getTextOfNode(node); + let type = stringLiteralTypes[text] = createType(TypeFlags.StringLiteral); + type.text = text; return type; } From 20c2c4e5e53ce5c19667caf087d303530df1e432 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 15:22:35 -0700 Subject: [PATCH 11/38] Amended fourslash tests to expect double quotes. --- .../fourslash/overloadOnConstCallSignature.ts | 2 +- .../fourslash/quickInfoForOverloadOnConst1.ts | 16 ++++++++-------- .../fourslash/signatureHelpOnOverloadOnConst.ts | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/cases/fourslash/overloadOnConstCallSignature.ts b/tests/cases/fourslash/overloadOnConstCallSignature.ts index d729fd4da6f8d..101d7750e0424 100644 --- a/tests/cases/fourslash/overloadOnConstCallSignature.ts +++ b/tests/cases/fourslash/overloadOnConstCallSignature.ts @@ -11,7 +11,7 @@ goTo.marker('1'); verify.signatureHelpCountIs(4); -verify.currentSignatureHelpIs('foo(name: \'order\'): string'); +verify.currentSignatureHelpIs('foo(name: "order"): string'); edit.insert('"hi"'); goTo.marker('2'); diff --git a/tests/cases/fourslash/quickInfoForOverloadOnConst1.ts b/tests/cases/fourslash/quickInfoForOverloadOnConst1.ts index f2ee37aa675db..948c2fde99911 100644 --- a/tests/cases/fourslash/quickInfoForOverloadOnConst1.ts +++ b/tests/cases/fourslash/quickInfoForOverloadOnConst1.ts @@ -20,22 +20,22 @@ ////c.x1(1, (x/*10*/x) => { return 1; } ); goTo.marker('1'); -verify.quickInfoIs("(method) I.x1(a: number, callback: (x: 'hi') => number): any"); +verify.quickInfoIs("(method) I.x1(a: number, callback: (x: \"hi\") => number): any"); goTo.marker('2'); -verify.quickInfoIs("(method) C.x1(a: number, callback: (x: 'hi') => number): any"); +verify.quickInfoIs("(method) C.x1(a: number, callback: (x: \"hi\") => number): any"); goTo.marker('3'); -verify.quickInfoIs("(parameter) callback: (x: 'hi') => number"); +verify.quickInfoIs("(parameter) callback: (x: \"hi\") => number"); goTo.marker('4'); -verify.quickInfoIs("(method) C.x1(a: number, callback: (x: 'hi') => number): any"); +verify.quickInfoIs("(method) C.x1(a: number, callback: (x: \"hi\") => number): any"); goTo.marker('5'); verify.quickInfoIs('(parameter) callback: (x: string) => number'); goTo.marker('6'); verify.quickInfoIs('(parameter) callback: (x: string) => number'); goTo.marker('7'); -verify.quickInfoIs("(method) C.x1(a: number, callback: (x: 'hi') => number): any"); +verify.quickInfoIs("(method) C.x1(a: number, callback: (x: \"hi\") => number): any"); goTo.marker('8'); -verify.quickInfoIs("(parameter) xx: 'hi'"); +verify.quickInfoIs("(parameter) xx: \"hi\""); goTo.marker('9'); -verify.quickInfoIs("(parameter) xx: 'bye'"); +verify.quickInfoIs("(parameter) xx: \"bye\""); goTo.marker('10'); -verify.quickInfoIs("(parameter) xx: 'hi'"); \ No newline at end of file +verify.quickInfoIs("(parameter) xx: \"hi\""); \ No newline at end of file diff --git a/tests/cases/fourslash/signatureHelpOnOverloadOnConst.ts b/tests/cases/fourslash/signatureHelpOnOverloadOnConst.ts index 8edd0617d70ab..e312b71aebe58 100644 --- a/tests/cases/fourslash/signatureHelpOnOverloadOnConst.ts +++ b/tests/cases/fourslash/signatureHelpOnOverloadOnConst.ts @@ -18,9 +18,9 @@ verify.currentParameterSpanIs("z: string"); goTo.marker('2'); verify.signatureHelpCountIs(3); verify.currentParameterHelpArgumentNameIs("x"); -verify.currentParameterSpanIs("x: 'hi'"); +verify.currentParameterSpanIs("x: \"hi\""); goTo.marker('3'); verify.signatureHelpCountIs(3); verify.currentParameterHelpArgumentNameIs("y"); -verify.currentParameterSpanIs("y: 'bye'"); +verify.currentParameterSpanIs("y: \"bye\""); From 87f2957e4d510637f5fcab4151561a60548944d0 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 15:23:53 -0700 Subject: [PATCH 12/38] Accepted baselines. --- .../callSignatureFunctionOverload.types | 16 +-- .../reference/constantOverloadFunction.types | 16 +-- ...ritedOverloadedSpecializedSignatures.types | 16 +-- ...pecializedCallAndConstructSignatures.types | 4 +- .../memberFunctionsWithPublicOverloads.types | 40 +++---- .../overloadOnConstConstraintChecks1.types | 30 +++--- .../overloadOnConstConstraintChecks2.types | 12 +-- .../overloadOnConstConstraintChecks3.types | 12 +-- .../overloadOnConstInheritance1.types | 12 +-- .../overloadOnConstInheritance2.errors.txt | 4 +- .../overloadOnConstInheritance3.errors.txt | 4 +- .../reference/primtiveTypesAreIdentical.types | 12 +-- .../specializedSignatureInInterface.types | 4 +- ...reIsSubtypeOfNonSpecializedSignature.types | 102 +++++++++--------- .../reference/stringLiteralType.types | 10 +- .../stringLiteralTypesOverloads01.errors.txt | 4 +- .../stringLiteralTypesOverloads01.js | 6 +- ...aturesWithSpecializedSignatures.errors.txt | 4 +- ...aturesWithSpecializedSignatures.errors.txt | 4 +- .../typesWithSpecializedCallSignatures.types | 58 +++++----- ...esWithSpecializedConstructSignatures.types | 26 ++--- 21 files changed, 198 insertions(+), 198 deletions(-) diff --git a/tests/baselines/reference/callSignatureFunctionOverload.types b/tests/baselines/reference/callSignatureFunctionOverload.types index b9788e5b143a9..d5897c01494ca 100644 --- a/tests/baselines/reference/callSignatureFunctionOverload.types +++ b/tests/baselines/reference/callSignatureFunctionOverload.types @@ -1,33 +1,33 @@ === tests/cases/compiler/callSignatureFunctionOverload.ts === var foo: { ->foo : { (name: string): string; (name: 'order'): string; (name: 'content'): string; (name: 'done'): string; } +>foo : { (name: string): string; (name: "order"): string; (name: "content"): string; (name: "done"): string; } (name: string): string; >name : string (name: 'order'): string; ->name : 'order' +>name : "order" (name: 'content'): string; ->name : 'content' +>name : "content" (name: 'done'): string; ->name : 'done' +>name : "done" } var foo2: { ->foo2 : { (name: string): string; (name: 'order'): string; (name: 'order'): string; (name: 'done'): string; } +>foo2 : { (name: string): string; (name: "order"): string; (name: "order"): string; (name: "done"): string; } (name: string): string; >name : string (name: 'order'): string; ->name : 'order' +>name : "order" (name: 'order'): string; ->name : 'order' +>name : "order" (name: 'done'): string; ->name : 'done' +>name : "done" } diff --git a/tests/baselines/reference/constantOverloadFunction.types b/tests/baselines/reference/constantOverloadFunction.types index d643d3a726d9a..d7deaaae46d79 100644 --- a/tests/baselines/reference/constantOverloadFunction.types +++ b/tests/baselines/reference/constantOverloadFunction.types @@ -19,27 +19,27 @@ class Derived3 extends Base { biz() { } } >biz : () => void function foo(tagName: 'canvas'): Derived1; ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } ->tagName : 'canvas' +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } +>tagName : "canvas" >Derived1 : Derived1 function foo(tagName: 'div'): Derived2; ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } ->tagName : 'div' +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } +>tagName : "div" >Derived2 : Derived2 function foo(tagName: 'span'): Derived3; ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } ->tagName : 'span' +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } +>tagName : "span" >Derived3 : Derived3 function foo(tagName: string): Base; ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } >tagName : string >Base : Base function foo(tagName: any): Base { ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } >tagName : any >Base : Base diff --git a/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types b/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types index dc8b9b465d81f..0ec428bb159fc 100644 --- a/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types +++ b/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types @@ -11,7 +11,7 @@ interface B extends A { >A : A (key:'foo'):string; ->key : 'foo' +>key : "foo" } var b:B; @@ -32,7 +32,7 @@ interface A { >A : A (x: 'A1'): string; ->x : 'A1' +>x : "A1" (x: string): void; >x : string @@ -43,21 +43,21 @@ interface B extends A { >A : A (x: 'B1'): number; ->x : 'B1' +>x : "B1" } interface A { >A : A (x: 'A2'): boolean; ->x : 'A2' +>x : "A2" } interface B { >B : B (x: 'B2'): string[]; ->x : 'B2' +>x : "B2" } interface C1 extends B { @@ -65,7 +65,7 @@ interface C1 extends B { >B : B (x: 'C1'): number[]; ->x : 'C1' +>x : "C1" } interface C2 extends B { @@ -73,7 +73,7 @@ interface C2 extends B { >B : B (x: 'C2'): boolean[]; ->x : 'C2' +>x : "C2" } interface C extends C1, C2 { @@ -82,7 +82,7 @@ interface C extends C1, C2 { >C2 : C2 (x: 'C'): string; ->x : 'C' +>x : "C" } var c: C; diff --git a/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types b/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types index 971b5e56907fa..7b19fd5889b31 100644 --- a/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types +++ b/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types @@ -3,13 +3,13 @@ interface Foo { >Foo : Foo (x: 'a'): number; ->x : 'a' +>x : "a" (x: string): any; >x : string new (x: 'a'): any; ->x : 'a' +>x : "a" new (x: string): Object; >x : string diff --git a/tests/baselines/reference/memberFunctionsWithPublicOverloads.types b/tests/baselines/reference/memberFunctionsWithPublicOverloads.types index 7cbc2aca1ffb8..bcd937e9cfc2b 100644 --- a/tests/baselines/reference/memberFunctionsWithPublicOverloads.types +++ b/tests/baselines/reference/memberFunctionsWithPublicOverloads.types @@ -17,20 +17,20 @@ class C { >y : any public bar(x: 'hi'); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } ->x : 'hi' +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } +>x : "hi" public bar(x: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : string public bar(x: number, y: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : number >y : string public bar(x: any, y?: any) { } ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : any >y : any @@ -49,20 +49,20 @@ class C { >y : any public static bar(x: 'hi'); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } ->x : 'hi' +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } +>x : "hi" public static bar(x: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : string public static bar(x: number, y: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : number >y : string public static bar(x: any, y?: any) { } ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : any >y : any } @@ -88,22 +88,22 @@ class D { >y : any public bar(x: 'hi'); ->bar : { (x: 'hi'): any; (x: string): any; (x: T, y: T): any; } ->x : 'hi' +>bar : { (x: "hi"): any; (x: string): any; (x: T, y: T): any; } +>x : "hi" public bar(x: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: T, y: T): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: T, y: T): any; } >x : string public bar(x: T, y: T); ->bar : { (x: 'hi'): any; (x: string): any; (x: T, y: T): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: T, y: T): any; } >x : T >T : T >y : T >T : T public bar(x: any, y?: any) { } ->bar : { (x: 'hi'): any; (x: string): any; (x: T, y: T): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: T, y: T): any; } >x : any >y : any @@ -122,20 +122,20 @@ class D { >y : any public static bar(x: 'hi'); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } ->x : 'hi' +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } +>x : "hi" public static bar(x: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : string public static bar(x: number, y: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : number >y : string public static bar(x: any, y?: any) { } ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : any >y : any diff --git a/tests/baselines/reference/overloadOnConstConstraintChecks1.types b/tests/baselines/reference/overloadOnConstConstraintChecks1.types index cc561eb6e21c6..012774b3cc9eb 100644 --- a/tests/baselines/reference/overloadOnConstConstraintChecks1.types +++ b/tests/baselines/reference/overloadOnConstConstraintChecks1.types @@ -22,23 +22,23 @@ interface MyDoc { // Document >MyDoc : MyDoc createElement(tagName: string): Base; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } >tagName : string >Base : Base createElement(tagName: 'canvas'): Derived1; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'canvas' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "canvas" >Derived1 : Derived1 createElement(tagName: 'div'): Derived2; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'div' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "div" >Derived2 : Derived2 createElement(tagName: 'span'): Derived3; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'span' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "span" >Derived3 : Derived3 // + 100 more @@ -49,27 +49,27 @@ class D implements MyDoc { >MyDoc : MyDoc createElement(tagName:string): Base; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } >tagName : string >Base : Base createElement(tagName: 'canvas'): Derived1; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'canvas' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "canvas" >Derived1 : Derived1 createElement(tagName: 'div'): Derived2; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'div' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "div" >Derived2 : Derived2 createElement(tagName: 'span'): Derived3; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'span' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "span" >Derived3 : Derived3 createElement(tagName:any): Base { ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } >tagName : any >Base : Base diff --git a/tests/baselines/reference/overloadOnConstConstraintChecks2.types b/tests/baselines/reference/overloadOnConstConstraintChecks2.types index 36fa89dec4a5a..6102c282a60a3 100644 --- a/tests/baselines/reference/overloadOnConstConstraintChecks2.types +++ b/tests/baselines/reference/overloadOnConstConstraintChecks2.types @@ -14,22 +14,22 @@ class C extends A { >foo : () => void } function foo(name: 'hi'): B; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } ->name : 'hi' +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } +>name : "hi" >B : B function foo(name: 'bye'): C; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } ->name : 'bye' +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } +>name : "bye" >C : C function foo(name: string): A; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } >name : string >A : A function foo(name: any): A { ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } >name : any >A : A diff --git a/tests/baselines/reference/overloadOnConstConstraintChecks3.types b/tests/baselines/reference/overloadOnConstConstraintChecks3.types index 94a03bb768070..0348d3d4b79b8 100644 --- a/tests/baselines/reference/overloadOnConstConstraintChecks3.types +++ b/tests/baselines/reference/overloadOnConstConstraintChecks3.types @@ -16,22 +16,22 @@ class C extends A { >foo : () => void } function foo(name: 'hi'): B; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } ->name : 'hi' +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } +>name : "hi" >B : B function foo(name: 'bye'): C; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } ->name : 'bye' +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } +>name : "bye" >C : C function foo(name: string): A; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } >name : string >A : A function foo(name: any): A { ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } >name : any >A : A diff --git a/tests/baselines/reference/overloadOnConstInheritance1.types b/tests/baselines/reference/overloadOnConstInheritance1.types index 96f8df3dffdf9..4721c18e55c8a 100644 --- a/tests/baselines/reference/overloadOnConstInheritance1.types +++ b/tests/baselines/reference/overloadOnConstInheritance1.types @@ -3,23 +3,23 @@ interface Base { >Base : Base addEventListener(x: string): any; ->addEventListener : { (x: string): any; (x: 'foo'): string; } +>addEventListener : { (x: string): any; (x: "foo"): string; } >x : string addEventListener(x: 'foo'): string; ->addEventListener : { (x: string): any; (x: 'foo'): string; } ->x : 'foo' +>addEventListener : { (x: string): any; (x: "foo"): string; } +>x : "foo" } interface Deriver extends Base { >Deriver : Deriver >Base : Base addEventListener(x: string): any; ->addEventListener : { (x: string): any; (x: 'bar'): string; } +>addEventListener : { (x: string): any; (x: "bar"): string; } >x : string addEventListener(x: 'bar'): string; ->addEventListener : { (x: string): any; (x: 'bar'): string; } ->x : 'bar' +>addEventListener : { (x: string): any; (x: "bar"): string; } +>x : "bar" } diff --git a/tests/baselines/reference/overloadOnConstInheritance2.errors.txt b/tests/baselines/reference/overloadOnConstInheritance2.errors.txt index 159af64a13ae2..4870106f4745b 100644 --- a/tests/baselines/reference/overloadOnConstInheritance2.errors.txt +++ b/tests/baselines/reference/overloadOnConstInheritance2.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/overloadOnConstInheritance2.ts(5,11): error TS2430: Interface 'Deriver' incorrectly extends interface 'Base'. Types of property 'addEventListener' are incompatible. - Type '(x: 'bar') => string' is not assignable to type '{ (x: string): any; (x: 'foo'): string; }'. + Type '(x: "bar") => string' is not assignable to type '{ (x: string): any; (x: "foo"): string; }'. tests/cases/compiler/overloadOnConstInheritance2.ts(6,5): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. @@ -13,7 +13,7 @@ tests/cases/compiler/overloadOnConstInheritance2.ts(6,5): error TS2382: Speciali ~~~~~~~ !!! error TS2430: Interface 'Deriver' incorrectly extends interface 'Base'. !!! error TS2430: Types of property 'addEventListener' are incompatible. -!!! error TS2430: Type '(x: 'bar') => string' is not assignable to type '{ (x: string): any; (x: 'foo'): string; }'. +!!! error TS2430: Type '(x: "bar") => string' is not assignable to type '{ (x: string): any; (x: "foo"): string; }'. addEventListener(x: 'bar'): string; // shouldn't need to redeclare the string overload ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. diff --git a/tests/baselines/reference/overloadOnConstInheritance3.errors.txt b/tests/baselines/reference/overloadOnConstInheritance3.errors.txt index e0fff7b1ec73c..b338f9795d79c 100644 --- a/tests/baselines/reference/overloadOnConstInheritance3.errors.txt +++ b/tests/baselines/reference/overloadOnConstInheritance3.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/overloadOnConstInheritance3.ts(4,11): error TS2430: Interface 'Deriver' incorrectly extends interface 'Base'. Types of property 'addEventListener' are incompatible. - Type '{ (x: 'bar'): string; (x: 'foo'): string; }' is not assignable to type '(x: string) => any'. + Type '{ (x: "bar"): string; (x: "foo"): string; }' is not assignable to type '(x: string) => any'. tests/cases/compiler/overloadOnConstInheritance3.ts(6,5): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/compiler/overloadOnConstInheritance3.ts(7,5): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. @@ -13,7 +13,7 @@ tests/cases/compiler/overloadOnConstInheritance3.ts(7,5): error TS2382: Speciali ~~~~~~~ !!! error TS2430: Interface 'Deriver' incorrectly extends interface 'Base'. !!! error TS2430: Types of property 'addEventListener' are incompatible. -!!! error TS2430: Type '{ (x: 'bar'): string; (x: 'foo'): string; }' is not assignable to type '(x: string) => any'. +!!! error TS2430: Type '{ (x: "bar"): string; (x: "foo"): string; }' is not assignable to type '(x: string) => any'. // shouldn't need to redeclare the string overload addEventListener(x: 'bar'): string; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/baselines/reference/primtiveTypesAreIdentical.types b/tests/baselines/reference/primtiveTypesAreIdentical.types index bbff4623f0b88..0b8e7dadf7dc3 100644 --- a/tests/baselines/reference/primtiveTypesAreIdentical.types +++ b/tests/baselines/reference/primtiveTypesAreIdentical.types @@ -50,19 +50,19 @@ function foo4(x: any) { } >x : any function foo5(x: 'a'); ->foo5 : { (x: 'a'): any; (x: 'a'): any; (x: string): any; } ->x : 'a' +>foo5 : { (x: "a"): any; (x: "a"): any; (x: string): any; } +>x : "a" function foo5(x: 'a'); ->foo5 : { (x: 'a'): any; (x: 'a'): any; (x: string): any; } ->x : 'a' +>foo5 : { (x: "a"): any; (x: "a"): any; (x: string): any; } +>x : "a" function foo5(x: string); ->foo5 : { (x: 'a'): any; (x: 'a'): any; (x: string): any; } +>foo5 : { (x: "a"): any; (x: "a"): any; (x: string): any; } >x : string function foo5(x: any) { } ->foo5 : { (x: 'a'): any; (x: 'a'): any; (x: string): any; } +>foo5 : { (x: "a"): any; (x: "a"): any; (x: string): any; } >x : any enum E { A } diff --git a/tests/baselines/reference/specializedSignatureInInterface.types b/tests/baselines/reference/specializedSignatureInInterface.types index c8f76f4cb9b09..b9621af7b8be0 100644 --- a/tests/baselines/reference/specializedSignatureInInterface.types +++ b/tests/baselines/reference/specializedSignatureInInterface.types @@ -11,8 +11,8 @@ interface B extends A { >A : A (key:'foo'):string; ->key : 'foo' +>key : "foo" (key:'bar'):string; ->key : 'bar' +>key : "bar" } diff --git a/tests/baselines/reference/specializedSignatureIsSubtypeOfNonSpecializedSignature.types b/tests/baselines/reference/specializedSignatureIsSubtypeOfNonSpecializedSignature.types index cb86966d65a0b..b27a935ced687 100644 --- a/tests/baselines/reference/specializedSignatureIsSubtypeOfNonSpecializedSignature.types +++ b/tests/baselines/reference/specializedSignatureIsSubtypeOfNonSpecializedSignature.types @@ -3,30 +3,30 @@ // All the below should not be errors function foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; } +>x : "a" function foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; } +>foo : { (x: "a"): any; (x: string): any; } >x : string function foo(x: any) { } ->foo : { (x: 'a'): any; (x: string): any; } +>foo : { (x: "a"): any; (x: string): any; } >x : any class C { >C : C foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; } +>foo : { (x: "a"): any; (x: string): any; } >x : string foo(x: any) { } ->foo : { (x: 'a'): any; (x: string): any; } +>foo : { (x: "a"): any; (x: string): any; } >x : any } @@ -35,20 +35,20 @@ class C2 { >T : T foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : string foo(x: T); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : T >T : T foo(x: any) { } ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : any } @@ -58,20 +58,20 @@ class C3 { >String : String foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : string foo(x: T); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : T >T : T foo(x: any) { } ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : any } @@ -79,7 +79,7 @@ interface I { >I : I (x: 'a'); ->x : 'a' +>x : "a" (x: number); >x : number @@ -88,15 +88,15 @@ interface I { >x : string foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: number): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: number): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: number): any; } +>foo : { (x: "a"): any; (x: string): any; (x: number): any; } >x : string foo(x: number); ->foo : { (x: 'a'): any; (x: string): any; (x: number): any; } +>foo : { (x: "a"): any; (x: string): any; (x: number): any; } >x : number } @@ -105,7 +105,7 @@ interface I2 { >T : T (x: 'a'); ->x : 'a' +>x : "a" (x: T); >x : T @@ -115,15 +115,15 @@ interface I2 { >x : string foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : string foo(x: T); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : T >T : T } @@ -134,7 +134,7 @@ interface I3 { >String : String (x: 'a'); ->x : 'a' +>x : "a" (x: string); >x : string @@ -144,49 +144,49 @@ interface I3 { >T : T foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : string foo(x: T); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : T >T : T } var a: { ->a : { (x: string): any; (x: 'a'): any; (x: number): any; foo(x: string): any; foo(x: 'a'): any; foo(x: number): any; } +>a : { (x: string): any; (x: "a"): any; (x: number): any; foo(x: string): any; foo(x: "a"): any; foo(x: number): any; } (x: string); >x : string (x: 'a'); ->x : 'a' +>x : "a" (x: number); >x : number foo(x: string); ->foo : { (x: string): any; (x: 'a'): any; (x: number): any; } +>foo : { (x: string): any; (x: "a"): any; (x: number): any; } >x : string foo(x: 'a'); ->foo : { (x: string): any; (x: 'a'): any; (x: number): any; } ->x : 'a' +>foo : { (x: string): any; (x: "a"): any; (x: number): any; } +>x : "a" foo(x: number); ->foo : { (x: string): any; (x: 'a'): any; (x: number): any; } +>foo : { (x: string): any; (x: "a"): any; (x: number): any; } >x : number } var a2: { ->a2 : { (x: 'a'): any; (x: string): any; (x: T): any; foo(x: string): any; foo(x: 'a'): any; foo(x: T): any; } +>a2 : { (x: "a"): any; (x: string): any; (x: T): any; foo(x: string): any; foo(x: "a"): any; foo(x: T): any; } (x: 'a'); ->x : 'a' +>x : "a" (x: string); >x : string @@ -197,25 +197,25 @@ var a2: { >T : T foo(x: string); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } >x : string foo(x: 'a'); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } ->x : 'a' +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } +>x : "a" foo(x: T); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } >T : T >x : T >T : T } var a3: { ->a3 : { (x: 'a'): any; (x: T): any; (x: string): any; foo(x: string): any; foo(x: 'a'): any; foo(x: T): any; } +>a3 : { (x: "a"): any; (x: T): any; (x: string): any; foo(x: string): any; foo(x: "a"): any; foo(x: T): any; } (x: 'a'); ->x : 'a' +>x : "a" (x: T); >T : T @@ -226,15 +226,15 @@ var a3: { >x : string foo(x: string); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } >x : string foo(x: 'a'); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } ->x : 'a' +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } +>x : "a" foo(x: T); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } >T : T >String : String >x : T diff --git a/tests/baselines/reference/stringLiteralType.types b/tests/baselines/reference/stringLiteralType.types index 5d2e13c4ba5fa..affa995c2f30f 100644 --- a/tests/baselines/reference/stringLiteralType.types +++ b/tests/baselines/reference/stringLiteralType.types @@ -1,16 +1,16 @@ === tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts === var x: 'hi'; ->x : 'hi' +>x : "hi" function f(x: 'hi'); ->f : { (x: 'hi'): any; (x: string): any; } ->x : 'hi' +>f : { (x: "hi"): any; (x: string): any; } +>x : "hi" function f(x: string); ->f : { (x: 'hi'): any; (x: string): any; } +>f : { (x: "hi"): any; (x: string): any; } >x : string function f(x: any) { ->f : { (x: 'hi'): any; (x: string): any; } +>f : { (x: "hi"): any; (x: string): any; } >x : any } diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt index 378857dc2771f..6e0fd982638fd 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(11,10): error TS2354: No best common type exists among return expressions. tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,7): error TS2322: Type 'string' is not assignable to type '"string"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,7): error TS2322: Type 'string' is not assignable to type ''number''. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,7): error TS2322: Type 'string' is not assignable to type '"number"'. tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,7): error TS2322: Type 'string' is not assignable to type '"boolean"'. @@ -43,7 +43,7 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34, !!! error TS2322: Type 'string' is not assignable to type '"string"'. const number: "number" = "number" ~~~~~~ -!!! error TS2322: Type 'string' is not assignable to type ''number''. +!!! error TS2322: Type 'string' is not assignable to type '"number"'. const boolean: "boolean" = "boolean" ~~~~~~~ !!! error TS2322: Type 'string' is not assignable to type '"boolean"'. diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.js b/tests/baselines/reference/stringLiteralTypesOverloads01.js index 23483776a9b88..73dabd26fab18 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads01.js +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.js @@ -106,9 +106,9 @@ declare namespace Consts1 { declare const string: "string"; declare const number: "number"; declare const boolean: "boolean"; -declare const stringOrNumber: "string" | 'number'; +declare const stringOrNumber: "string" | "number"; declare const stringOrBoolean: "string" | "boolean"; -declare const booleanOrNumber: 'number' | "boolean"; -declare const stringOrBooleanOrNumber: "string" | "boolean" | 'number'; +declare const booleanOrNumber: "number" | "boolean"; +declare const stringOrBooleanOrNumber: "string" | "boolean" | "number"; declare namespace Consts2 { } diff --git a/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt b/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt index d3c399477d66c..da742fcd69cbd 100644 --- a/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt +++ b/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithCallSignaturesWithSpecializedSignatures.ts(70,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. Types of property 'a' are incompatible. - Type '(x: string) => string' is not assignable to type '{ (x: 'a'): number; (x: string): number; }'. + Type '(x: string) => string' is not assignable to type '{ (x: "a"): number; (x: string): number; }'. Type 'string' is not assignable to type 'number'. @@ -78,7 +78,7 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. !!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type '(x: string) => string' is not assignable to type '{ (x: 'a'): number; (x: string): number; }'. +!!! error TS2430: Type '(x: string) => string' is not assignable to type '{ (x: "a"): number; (x: string): number; }'. !!! error TS2430: Type 'string' is not assignable to type 'number'. // N's a: (x: string) => string; // error because base returns non-void; diff --git a/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt b/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt index 03cc6de0ca4b4..8a885a2455fb2 100644 --- a/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt +++ b/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithConstructSignaturesWithSpecializedSignatures.ts(70,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. Types of property 'a' are incompatible. - Type 'new (x: string) => string' is not assignable to type '{ new (x: 'a'): number; new (x: string): number; }'. + Type 'new (x: string) => string' is not assignable to type '{ new (x: "a"): number; new (x: string): number; }'. Type 'string' is not assignable to type 'number'. @@ -78,7 +78,7 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. !!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'new (x: string) => string' is not assignable to type '{ new (x: 'a'): number; new (x: string): number; }'. +!!! error TS2430: Type 'new (x: string) => string' is not assignable to type '{ new (x: "a"): number; new (x: string): number; }'. !!! error TS2430: Type 'string' is not assignable to type 'number'. // N's a: new (x: string) => string; // error because base returns non-void; diff --git a/tests/baselines/reference/typesWithSpecializedCallSignatures.types b/tests/baselines/reference/typesWithSpecializedCallSignatures.types index fa3ac7a4ba4d4..113e24456f984 100644 --- a/tests/baselines/reference/typesWithSpecializedCallSignatures.types +++ b/tests/baselines/reference/typesWithSpecializedCallSignatures.types @@ -19,22 +19,22 @@ class C { >C : C foo(x: 'hi'): Derived1; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'hi' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "hi" >Derived1 : Derived1 foo(x: 'bye'): Derived2; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'bye' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "bye" >Derived2 : Derived2 foo(x: string): Base; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >x : string >Base : Base foo(x) { ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >x : any return x; @@ -50,17 +50,17 @@ interface I { >I : I foo(x: 'hi'): Derived1; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'hi' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "hi" >Derived1 : Derived1 foo(x: 'bye'): Derived2; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'bye' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "bye" >Derived2 : Derived2 foo(x: string): Base; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >x : string >Base : Base } @@ -69,20 +69,20 @@ var i: I; >I : I var a: { ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } foo(x: 'hi'): Derived1; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'hi' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "hi" >Derived1 : Derived1 foo(x: 'bye'): Derived2; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'bye' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "bye" >Derived2 : Derived2 foo(x: string): Base; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >x : string >Base : Base @@ -94,9 +94,9 @@ c = i; >i : I c = a; ->c = a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>c = a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } >c : C ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } i = c; >i = c : C @@ -104,44 +104,44 @@ i = c; >c : C i = a; ->i = a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>i = a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } >i : I ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } a = c; >a = c : C ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } >c : C a = i; >a = i : I ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } >i : I var r1: Derived1 = c.foo('hi'); >r1 : Derived1 >Derived1 : Derived1 >c.foo('hi') : Derived1 ->c.foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>c.foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >c : C ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >'hi' : string var r2: Derived2 = c.foo('bye'); >r2 : Derived2 >Derived2 : Derived2 >c.foo('bye') : Derived2 ->c.foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>c.foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >c : C ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >'bye' : string var r3: Base = c.foo('hm'); >r3 : Base >Base : Base >c.foo('hm') : Base ->c.foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>c.foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >c : C ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >'hm' : string diff --git a/tests/baselines/reference/typesWithSpecializedConstructSignatures.types b/tests/baselines/reference/typesWithSpecializedConstructSignatures.types index 66e274d917adc..2698439c527af 100644 --- a/tests/baselines/reference/typesWithSpecializedConstructSignatures.types +++ b/tests/baselines/reference/typesWithSpecializedConstructSignatures.types @@ -19,10 +19,10 @@ class C { >C : C constructor(x: 'hi'); ->x : 'hi' +>x : "hi" constructor(x: 'bye'); ->x : 'bye' +>x : "bye" constructor(x: string); >x : string @@ -44,11 +44,11 @@ interface I { >I : I new(x: 'hi'): Derived1; ->x : 'hi' +>x : "hi" >Derived1 : Derived1 new(x: 'bye'): Derived2; ->x : 'bye' +>x : "bye" >Derived2 : Derived2 new(x: string): Base; @@ -60,14 +60,14 @@ var i: I; >I : I var a: { ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } new(x: 'hi'): Derived1; ->x : 'hi' +>x : "hi" >Derived1 : Derived1 new(x: 'bye'): Derived2; ->x : 'bye' +>x : "bye" >Derived2 : Derived2 new(x: string): Base; @@ -82,18 +82,18 @@ c = i; >i : I c = a; ->c = a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>c = a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } >c : C ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } i = a; ->i = a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>i = a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } >i : I ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } a = i; >a = i : I ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } >i : I var r1 = new C('hi'); @@ -113,6 +113,6 @@ var r3: Base = new a('hm'); >r3 : Base >Base : Base >new a('hm') : Base ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } >'hm' : string From f721971063f210389dc3cd4c0d0a6b3e4562d4e8 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 15:41:12 -0700 Subject: [PATCH 13/38] Capture compatible contextual types for unions containing string literals. --- src/compiler/checker.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c062398c1a95a..dd6c3a63df9ca 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10103,6 +10103,28 @@ namespace ts { return getUnionType([type1, type2]); } + function checkStringLiteralExpression(node: LiteralExpression) { + // TODO (drosen): Do we want to apply the same approach to no-sub template literals? + + let contextualType = getContextualType(node); + if (contextualType) { + if (contextualType.flags & TypeFlags.Union) { + for (const type of (contextualType).types) { + if (type.flags & TypeFlags.StringLiteral && (type).text === node.text) { + return contextualType; + } + } + } + else if (contextualType.flags & TypeFlags.StringLiteral && (contextualType).text === node.text) { + // NOTE: This doesn't work because the contextual type of a string literal + // always gets its apparent type. + // Thus you'll always end up with 'String' instead of the literal. + return contextualType; + } + } + return stringType; + } + function checkTemplateExpression(node: TemplateExpression): Type { // We just want to check each expressions, but we are unconcerned with // the type of each expression, as any value may be coerced into a string. @@ -10233,8 +10255,9 @@ namespace ts { case SyntaxKind.TemplateExpression: return checkTemplateExpression(node); case SyntaxKind.StringLiteral: + return checkStringLiteralExpression(node); case SyntaxKind.NoSubstitutionTemplateLiteral: - return stringType; + return stringType case SyntaxKind.RegularExpressionLiteral: return globalRegExpType; case SyntaxKind.ArrayLiteralExpression: From 7b4e94dbd57aef497a6cda2d6b440d35dd6e9d19 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 15:42:16 -0700 Subject: [PATCH 14/38] Accepted baselines. --- ...tringLiteralTypesInUnionTypes01.errors.txt | 33 ------- .../stringLiteralTypesInUnionTypes01.symbols | 52 +++++++++++ .../stringLiteralTypesInUnionTypes01.types | 62 +++++++++++++ .../stringLiteralTypesInUnionTypes02.types | 4 +- ...tringLiteralTypesInUnionTypes03.errors.txt | 7 +- ...tringLiteralTypesInUnionTypes04.errors.txt | 50 ---------- .../stringLiteralTypesInUnionTypes04.symbols | 76 +++++++++++++++ .../stringLiteralTypesInUnionTypes04.types | 92 +++++++++++++++++++ ...ingLiteralTypesTypePredicates01.errors.txt | 7 +- 9 files changed, 286 insertions(+), 97 deletions(-) delete mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes01.symbols create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes01.types delete mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes04.symbols create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes04.types diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt deleted file mode 100644 index 14d0a892f55d0..0000000000000 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.errors.txt +++ /dev/null @@ -1,33 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(4,5): error TS2322: Type 'string' is not assignable to type '"foo" | "bar" | "baz"'. - Type 'string' is not assignable to type '"baz"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts(5,5): error TS2322: Type 'string' is not assignable to type '"foo" | "bar" | "baz"'. - Type 'string' is not assignable to type '"baz"'. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts (2 errors) ==== - - type T = "foo" | "bar" | "baz"; - - var x: "foo" | "bar" | "baz" = "foo"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"foo" | "bar" | "baz"'. -!!! error TS2322: Type 'string' is not assignable to type '"baz"'. - var y: T = "bar"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"foo" | "bar" | "baz"'. -!!! error TS2322: Type 'string' is not assignable to type '"baz"'. - - if (x === "foo") { - let a = x; - } - else if (x !== "bar") { - let b = x || y; - } - else { - let c = x; - let d = y; - let e: (typeof x) | (typeof y) = c || d; - } - - x = y; - y = x; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.symbols b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.symbols new file mode 100644 index 0000000000000..c608929383ed3 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.symbols @@ -0,0 +1,52 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts === + +type T = "foo" | "bar" | "baz"; +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes01.ts, 0, 0)) + +var x: "foo" | "bar" | "baz" = "foo"; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + +var y: T = "bar"; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes01.ts, 0, 0)) + +if (x === "foo") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + + let a = x; +>a : Symbol(a, Decl(stringLiteralTypesInUnionTypes01.ts, 7, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) +} +else if (x !== "bar") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + + let b = x || y; +>b : Symbol(b, Decl(stringLiteralTypesInUnionTypes01.ts, 10, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) +} +else { + let c = x; +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes01.ts, 13, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + + let d = y; +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes01.ts, 14, 7)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) + + let e: (typeof x) | (typeof y) = c || d; +>e : Symbol(e, Decl(stringLiteralTypesInUnionTypes01.ts, 15, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes01.ts, 13, 7)) +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes01.ts, 14, 7)) +} + +x = y; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) + +y = x; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types new file mode 100644 index 0000000000000..b5a2d876bcc11 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types @@ -0,0 +1,62 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts === + +type T = "foo" | "bar" | "baz"; +>T : "foo" | "bar" | "baz" + +var x: "foo" | "bar" | "baz" = "foo"; +>x : "foo" | "bar" | "baz" +>"foo" : "foo" | "bar" | "baz" + +var y: T = "bar"; +>y : "foo" | "bar" | "baz" +>T : "foo" | "bar" | "baz" +>"bar" : "foo" | "bar" | "baz" + +if (x === "foo") { +>x === "foo" : boolean +>x : "foo" | "bar" | "baz" +>"foo" : string + + let a = x; +>a : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" +} +else if (x !== "bar") { +>x !== "bar" : boolean +>x : "foo" | "bar" | "baz" +>"bar" : string + + let b = x || y; +>b : "foo" | "bar" | "baz" +>x || y : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" +} +else { + let c = x; +>c : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" + + let d = y; +>d : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" + + let e: (typeof x) | (typeof y) = c || d; +>e : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" +>c || d : "foo" | "bar" | "baz" +>c : "foo" | "bar" | "baz" +>d : "foo" | "bar" | "baz" +} + +x = y; +>x = y : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" + +y = x; +>y = x : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types index 569edc7781593..e3ac5ba481785 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types @@ -5,12 +5,12 @@ type T = string | "foo" | "bar" | "baz"; var x: "foo" | "bar" | "baz" | string = "foo"; >x : "foo" | "bar" | "baz" | string ->"foo" : string +>"foo" : "foo" | "bar" | "baz" | string var y: T = "bar"; >y : string | "foo" | "bar" | "baz" >T : string | "foo" | "bar" | "baz" ->"bar" : string +>"bar" : string | "foo" | "bar" | "baz" if (x === "foo") { >x === "foo" : boolean diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt index 8ff377b2e728e..74249b178d619 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt @@ -1,18 +1,13 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(5,5): error TS2322: Type 'string' is not assignable to type 'number | "foo" | "bar"'. - Type 'string' is not assignable to type '"bar"'. tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(7,5): error TS2365: Operator '===' cannot be applied to types '"foo" | "bar" | number' and 'string'. tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(10,10): error TS2365: Operator '!==' cannot be applied to types '"foo" | "bar" | number' and 'string'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts (3 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts (2 errors) ==== type T = number | "foo" | "bar"; var x: "foo" | "bar" | number; var y: T = "bar"; - ~ -!!! error TS2322: Type 'string' is not assignable to type 'number | "foo" | "bar"'. -!!! error TS2322: Type 'string' is not assignable to type '"bar"'. if (x === "foo") { ~~~~~~~~~~~ diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt deleted file mode 100644 index 77cb9c09531a7..0000000000000 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.errors.txt +++ /dev/null @@ -1,50 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(4,5): error TS2322: Type 'string' is not assignable to type '"" | "foo"'. - Type 'string' is not assignable to type '"foo"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts(5,5): error TS2322: Type 'string' is not assignable to type '"" | "foo"'. - Type 'string' is not assignable to type '"foo"'. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts (2 errors) ==== - - type T = "" | "foo"; - - let x: T = ""; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"" | "foo"'. -!!! error TS2322: Type 'string' is not assignable to type '"foo"'. - let y: T = "foo"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"" | "foo"'. -!!! error TS2322: Type 'string' is not assignable to type '"foo"'. - - if (x === "") { - let a = x; - } - - if (x !== "") { - let b = x; - } - - if (x == "") { - let c = x; - } - - if (x != "") { - let d = x; - } - - if (x) { - let e = x; - } - - if (!x) { - let f = x; - } - - if (!!x) { - let g = x; - } - - if (!!!x) { - let h = x; - } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.symbols b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.symbols new file mode 100644 index 0000000000000..ced93fcefc98f --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.symbols @@ -0,0 +1,76 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts === + +type T = "" | "foo"; +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes04.ts, 0, 0)) + +let x: T = ""; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes04.ts, 0, 0)) + +let y: T = "foo"; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes04.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes04.ts, 0, 0)) + +if (x === "") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let a = x; +>a : Symbol(a, Decl(stringLiteralTypesInUnionTypes04.ts, 7, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (x !== "") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let b = x; +>b : Symbol(b, Decl(stringLiteralTypesInUnionTypes04.ts, 11, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (x == "") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let c = x; +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes04.ts, 15, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (x != "") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let d = x; +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes04.ts, 19, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (x) { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let e = x; +>e : Symbol(e, Decl(stringLiteralTypesInUnionTypes04.ts, 23, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (!x) { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let f = x; +>f : Symbol(f, Decl(stringLiteralTypesInUnionTypes04.ts, 27, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (!!x) { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let g = x; +>g : Symbol(g, Decl(stringLiteralTypesInUnionTypes04.ts, 31, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (!!!x) { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let h = x; +>h : Symbol(h, Decl(stringLiteralTypesInUnionTypes04.ts, 35, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types new file mode 100644 index 0000000000000..9a010b490cca2 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types @@ -0,0 +1,92 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts === + +type T = "" | "foo"; +>T : "" | "foo" + +let x: T = ""; +>x : "" | "foo" +>T : "" | "foo" +>"" : "" | "foo" + +let y: T = "foo"; +>y : "" | "foo" +>T : "" | "foo" +>"foo" : "" | "foo" + +if (x === "") { +>x === "" : boolean +>x : "" | "foo" +>"" : string + + let a = x; +>a : "" | "foo" +>x : "" | "foo" +} + +if (x !== "") { +>x !== "" : boolean +>x : "" | "foo" +>"" : string + + let b = x; +>b : "" | "foo" +>x : "" | "foo" +} + +if (x == "") { +>x == "" : boolean +>x : "" | "foo" +>"" : string + + let c = x; +>c : "" | "foo" +>x : "" | "foo" +} + +if (x != "") { +>x != "" : boolean +>x : "" | "foo" +>"" : string + + let d = x; +>d : "" | "foo" +>x : "" | "foo" +} + +if (x) { +>x : "" | "foo" + + let e = x; +>e : "" | "foo" +>x : "" | "foo" +} + +if (!x) { +>!x : boolean +>x : "" | "foo" + + let f = x; +>f : "" | "foo" +>x : "" | "foo" +} + +if (!!x) { +>!!x : boolean +>!x : boolean +>x : "" | "foo" + + let g = x; +>g : "" | "foo" +>x : "" | "foo" +} + +if (!!!x) { +>!!!x : boolean +>!!x : boolean +>!x : boolean +>x : "" | "foo" + + let h = x; +>h : "" | "foo" +>x : "" | "foo" +} diff --git a/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt b/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt index 3e412a6c770f0..b888c3ca77e40 100644 --- a/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt @@ -1,10 +1,8 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(10,5): error TS2322: Type 'string' is not assignable to type '"A" | "B"'. - Type 'string' is not assignable to type '"B"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts (3 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts (2 errors) ==== type Kind = "A" | "B" @@ -19,9 +17,6 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.t } var x: Kind = "A"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"A" | "B"'. -!!! error TS2322: Type 'string' is not assignable to type '"B"'. if (kindIs(x, "A")) { let a = x; From d8d72aabae439e17d18e35887981165413b71556 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 16:00:45 -0700 Subject: [PATCH 15/38] Separated the concept of apparent types from contextual types for string literal types. In most cases, expressions are interested in the apparent type of the contextual type. For instance: var x = { hasOwnProperty(prop) { /* ... */ }; In the above, 'prop' should be contextually typed as 'string' from the signature of 'hasOwnProperty' in the global 'Object' type. However, in the case of string literal types, we don't want to get the apparent type after fetching the contextual type. This is because the apparent type of the '"onload"' string literal type is the global 'String' type. This has adverse effects in simple assignments like the following: let x: "onload" = "onload"; In this example, the right-hand side of the assignment will grab the type of 'x'. After figuring out the type is "onload", we then get the apparent type which is 'String'. This is problematic because when we then check the assignment itself, 'String's are not assignable to '"onload"'s. So in this case, we grab the contextual type *without* getting its apparent type. --- src/compiler/checker.ts | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index dd6c3a63df9ca..2dcded72ad466 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -80,7 +80,7 @@ namespace ts { symbolToString, getAugmentedPropertiesOfType, getRootSymbols, - getContextualType, + getContextualType: getApparentTypeOfContextualType, getFullyQualifiedName, getResolvedSignature, getConstantValue, @@ -6716,7 +6716,7 @@ namespace ts { else if (operator === SyntaxKind.BarBarToken) { // When an || expression has a contextual type, the operands are contextually typed by that type. When an || // expression has no contextual type, the right operand is contextually typed by the type of the left operand. - let type = getContextualType(binaryExpression); + let type = getApparentTypeOfContextualType(binaryExpression); if (!type && node === binaryExpression.right) { type = checkExpression(binaryExpression.left); } @@ -6788,7 +6788,7 @@ namespace ts { function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElement) { let objectLiteral = element.parent; - let type = getContextualType(objectLiteral); + let type = getApparentTypeOfContextualType(objectLiteral); if (type) { if (!hasDynamicName(element)) { // For a (non-symbol) computed property, there is no reason to look up the name @@ -6814,7 +6814,7 @@ namespace ts { // type of T. function getContextualTypeForElementExpression(node: Expression): Type { let arrayLiteral = node.parent; - let type = getContextualType(arrayLiteral); + let type = getApparentTypeOfContextualType(arrayLiteral); if (type) { let index = indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) @@ -6827,7 +6827,7 @@ namespace ts { // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type. function getContextualTypeForConditionalOperand(node: Expression): Type { let conditional = node.parent; - return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined; + return node === conditional.whenTrue || node === conditional.whenFalse ? getApparentTypeOfContextualType(conditional) : undefined; } function getContextualTypeForJsxExpression(expr: JsxExpression|JsxSpreadAttribute): Type { @@ -6852,12 +6852,22 @@ namespace ts { // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily // be "pushed" onto a node using the contextualType property. - function getContextualType(node: Expression): Type { - let type = getContextualTypeWorker(node); + function getApparentTypeOfContextualType(node: Expression): Type { + let type = getContextualType(node); return type && getApparentType(type); } - function getContextualTypeWorker(node: Expression): Type { + /** + * Woah! Do you really want to use this function? + * + * Unless you're trying to get the *non-apparent* type for a value-literal type, + * you probably meant to use 'getApparentTypeOfContextualType'. + * Otherwise this is slightly less useful. + * + * @param node the expression whose contextual type will be returned. + * @returns the contextual type of an expression. + */ + function getContextualType(node: Expression): Type { if (isInsideWithStatementBody(node)) { // We cannot answer semantic questions within a with block, do not proceed any further return undefined; @@ -6896,7 +6906,7 @@ namespace ts { Debug.assert(parent.parent.kind === SyntaxKind.TemplateExpression); return getContextualTypeForSubstitutionExpression(parent.parent, node); case SyntaxKind.ParenthesizedExpression: - return getContextualType(parent); + return getApparentTypeOfContextualType(parent); case SyntaxKind.JsxExpression: case SyntaxKind.JsxSpreadAttribute: return getContextualTypeForJsxExpression(parent); @@ -6936,7 +6946,7 @@ namespace ts { Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node)); let type = isObjectLiteralMethod(node) ? getContextualTypeForObjectLiteralMethod(node) - : getContextualType(node); + : getApparentTypeOfContextualType(node); if (!type) { return undefined; } @@ -7066,7 +7076,7 @@ namespace ts { type.pattern = node; return type; } - let contextualType = getContextualType(node); + let contextualType = getApparentTypeOfContextualType(node); if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { let pattern = contextualType.pattern; // If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting @@ -7157,7 +7167,7 @@ namespace ts { let propertiesTable: SymbolTable = {}; let propertiesArray: Symbol[] = []; - let contextualType = getContextualType(node); + let contextualType = getApparentTypeOfContextualType(node); let contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression); let inDestructuringPattern = isAssignmentTarget(node); @@ -10116,9 +10126,6 @@ namespace ts { } } else if (contextualType.flags & TypeFlags.StringLiteral && (contextualType).text === node.text) { - // NOTE: This doesn't work because the contextual type of a string literal - // always gets its apparent type. - // Thus you'll always end up with 'String' instead of the literal. return contextualType; } } @@ -10184,7 +10191,7 @@ namespace ts { if (isInferentialContext(contextualMapper)) { let signature = getSingleCallSignature(type); if (signature && signature.typeParameters) { - let contextualType = getContextualType(node); + let contextualType = getApparentTypeOfContextualType(node); if (contextualType) { let contextualSignature = getSingleCallSignature(contextualType); if (contextualSignature && !contextualSignature.typeParameters) { From 315b06dc99b6bcf6079e31f535d9ab3171d1d9ad Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 16:09:57 -0700 Subject: [PATCH 16/38] Accepted baselines. --- ...ritedOverloadedSpecializedSignatures.types | 16 ++++----- ...pecializedCallAndConstructSignatures.types | 4 +-- .../stringLiteralTypesAndTuples01.errors.txt | 9 +---- .../stringLiteralTypesAsTags01.errors.txt | 9 +---- ...alTypesInVariableDeclarations01.errors.txt | 34 ++----------------- .../stringLiteralTypesOverloads01.errors.txt | 11 +----- .../stringLiteralTypesOverloads02.errors.txt | 11 +----- .../reference/symbolProperty41.types | 2 +- ...gumentsWithStringLiteralTypes01.errors.txt | 18 +++++----- .../typesWithSpecializedCallSignatures.types | 4 +-- ...esWithSpecializedConstructSignatures.types | 4 +-- 11 files changed, 30 insertions(+), 92 deletions(-) diff --git a/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types b/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types index 0ec428bb159fc..563cb58a755e0 100644 --- a/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types +++ b/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types @@ -24,7 +24,7 @@ b('foo').charAt(0); >b('foo').charAt : (pos: number) => string >b('foo') : string >b : B ->'foo' : string +>'foo' : "foo" >charAt : (pos: number) => string >0 : number @@ -94,25 +94,25 @@ var x1: string[] = c('B2'); >x1 : string[] >c('B2') : string[] >c : C ->'B2' : string +>'B2' : "B2" var x2: number = c('B1'); >x2 : number >c('B1') : number >c : C ->'B1' : string +>'B1' : "B1" var x3: boolean = c('A2'); >x3 : boolean >c('A2') : boolean >c : C ->'A2' : string +>'A2' : "A2" var x4: string = c('A1'); >x4 : string >c('A1') : string >c : C ->'A1' : string +>'A1' : "A1" var x5: void = c('A0'); >x5 : void @@ -124,19 +124,19 @@ var x6: number[] = c('C1'); >x6 : number[] >c('C1') : number[] >c : C ->'C1' : string +>'C1' : "C1" var x7: boolean[] = c('C2'); >x7 : boolean[] >c('C2') : boolean[] >c : C ->'C2' : string +>'C2' : "C2" var x8: string = c('C'); >x8 : string >c('C') : string >c : C ->'C' : string +>'C' : "C" var x9: void = c('generic'); >x9 : void diff --git a/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types b/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types index 7b19fd5889b31..5288d9f15a434 100644 --- a/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types +++ b/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types @@ -24,7 +24,7 @@ var r = f('a'); >r : number >f('a') : number >f : Foo ->'a' : string +>'a' : "a" var r2 = f('A'); >r2 : any @@ -36,7 +36,7 @@ var r3 = new f('a'); >r3 : any >new f('a') : any >f : Foo ->'a' : string +>'a' : "a" var r4 = new f('A'); >r4 : Object diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt b/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt index 849f4922e45fc..6a8704ce5546f 100644 --- a/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt @@ -1,20 +1,13 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts(6,5): error TS2322: Type '[string, string, string]' is not assignable to type '["I'm", "a", any]'. - Types of property '0' are incompatible. - Type 'string' is not assignable to type '"I'm"'. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts(6,37): error TS2304: Cannot find name 'Dinosaur'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts (2 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts (1 errors) ==== // Should all be strings. let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; type RexOrRaptor = "t-rex" | "raptor" let [im, a, dinosaur]: ["I'm", "a", Dinosaur] = ['I\'m', 'a', 't-rex']; - ~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '[string, string, string]' is not assignable to type '["I'm", "a", any]'. -!!! error TS2322: Types of property '0' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type '"I'm"'. ~~~~~~~~ !!! error TS2304: Cannot find name 'Dinosaur'. diff --git a/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt index df05e25304fcb..6e4f8943fb6af 100644 --- a/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt @@ -2,12 +2,9 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(18,10) tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(19,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(20,10): error TS2394: Overload signature is not compatible with function implementation. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(22,21): error TS2304: Cannot find name 'is'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(25,5): error TS2322: Type '{ kind: string; a: number; }' is not assignable to type 'A'. - Types of property 'kind' are incompatible. - Type 'string' is not assignable to type '"A"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (5 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (4 errors) ==== type Kind = "A" | "B" @@ -41,10 +38,6 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(25,5): } let x: A = { - ~ -!!! error TS2322: Type '{ kind: string; a: number; }' is not assignable to type 'A'. -!!! error TS2322: Types of property 'kind' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type '"A"'. kind: "A", a: 100, } diff --git a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt index 3dd2adbbce08b..1a639a1c22284 100644 --- a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt @@ -1,17 +1,7 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(5,7): error TS1155: 'const' declarations must be initialized -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(7,1): error TS2322: Type 'string' is not assignable to type '""'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(8,1): error TS2322: Type 'string' is not assignable to type '"foo"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(9,1): error TS2322: Type 'string' is not assignable to type '"bar"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(11,5): error TS2322: Type 'string' is not assignable to type '""'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(12,5): error TS2322: Type 'string' is not assignable to type '"foo"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(13,5): error TS2322: Type 'string' is not assignable to type '"bar"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(14,7): error TS2322: Type 'string' is not assignable to type '"baz"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(16,1): error TS2322: Type 'string' is not assignable to type '""'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(17,1): error TS2322: Type 'string' is not assignable to type '"foo"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(18,1): error TS2322: Type 'string' is not assignable to type '"bar"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts (11 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts (1 errors) ==== let a: ""; var b: "foo"; @@ -21,34 +11,14 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarat !!! error TS1155: 'const' declarations must be initialized a = ""; - ~ -!!! error TS2322: Type 'string' is not assignable to type '""'. b = "foo"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"foo"'. c = "bar"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"bar"'. let e: "" = ""; - ~ -!!! error TS2322: Type 'string' is not assignable to type '""'. var f: "foo" = "foo"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"foo"'. let g: "bar" = "bar"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"bar"'. const h: "baz" = "baz"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"baz"'. e = ""; - ~ -!!! error TS2322: Type 'string' is not assignable to type '""'. f = "foo"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"foo"'. - g = "bar"; - ~ -!!! error TS2322: Type 'string' is not assignable to type '"bar"'. \ No newline at end of file + g = "bar"; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt index 6e0fd982638fd..1592427208b3d 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt @@ -1,10 +1,7 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(11,10): error TS2354: No best common type exists among return expressions. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(32,7): error TS2322: Type 'string' is not assignable to type '"string"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(33,7): error TS2322: Type 'string' is not assignable to type '"number"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34,7): error TS2322: Type 'string' is not assignable to type '"boolean"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts (4 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts (1 errors) ==== type PrimitiveName = 'string' | 'number' | 'boolean'; @@ -39,14 +36,8 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(34, } const string: "string" = "string" - ~~~~~~ -!!! error TS2322: Type 'string' is not assignable to type '"string"'. const number: "number" = "number" - ~~~~~~ -!!! error TS2322: Type 'string' is not assignable to type '"number"'. const boolean: "boolean" = "boolean" - ~~~~~~~ -!!! error TS2322: Type 'string' is not assignable to type '"boolean"'. const stringOrNumber = string || number; const stringOrBoolean = string || boolean; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt index ae9fb9ce4f67f..995cf687b702c 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt @@ -1,10 +1,7 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(9,10): error TS2354: No best common type exists among return expressions. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(30,7): error TS2322: Type 'string' is not assignable to type '"string"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(31,7): error TS2322: Type 'string' is not assignable to type '"number"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32,7): error TS2322: Type 'string' is not assignable to type '"boolean"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts (4 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts (1 errors) ==== function getFalsyPrimitive(x: "string"): string; function getFalsyPrimitive(x: "number"): number; @@ -37,14 +34,8 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(32, } const string: "string" = "string" - ~~~~~~ -!!! error TS2322: Type 'string' is not assignable to type '"string"'. const number: "number" = "number" - ~~~~~~ -!!! error TS2322: Type 'string' is not assignable to type '"number"'. const boolean: "boolean" = "boolean" - ~~~~~~~ -!!! error TS2322: Type 'string' is not assignable to type '"boolean"'. const stringOrNumber = string || number; const stringOrBoolean = string || boolean; diff --git a/tests/baselines/reference/symbolProperty41.types b/tests/baselines/reference/symbolProperty41.types index 61b71dfe24689..f312481ac0403 100644 --- a/tests/baselines/reference/symbolProperty41.types +++ b/tests/baselines/reference/symbolProperty41.types @@ -49,5 +49,5 @@ c[Symbol.iterator]("hello"); >Symbol.iterator : symbol >Symbol : SymbolConstructor >iterator : symbol ->"hello" : string +>"hello" : "hello" diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt index 413d21270039a..69f8d6581a1ae 100644 --- a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt @@ -14,9 +14,9 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 Type 'string' is not assignable to type '"World"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(48,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. Type 'string' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,34): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,34): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,52): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(61,5): error TS2322: Type 'string' is not assignable to type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(63,5): error TS2322: Type 'string' is not assignable to type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(75,5): error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. @@ -25,7 +25,7 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 Type '"World"' is not assignable to type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,52): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(93,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. Type 'string' is not assignable to type '"World"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(97,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. @@ -119,14 +119,14 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 // as "Hello" (or "Hello" | "Hello"). export let a = fun1<"Hello">("Hello", "Hello"); export let b = fun1<"Hello">("Hello", "World"); - ~~~~~~~ + ~~~~~~~ !!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. export let c = fun2<"Hello", "Hello">("Hello", "Hello"); export let d = fun2<"Hello", "Hello">("Hello", "World"); - ~~~~~~~ + ~~~~~~~ !!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. export let e = fun3<"Hello">("Hello", "World"); - ~~~~~~~ + ~~~~~~~ !!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. // Assignment from the returned value should cause an error. @@ -173,8 +173,8 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 ~~~~~~~ !!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. export let d = fun2<"World", "Hello">("World", "World"); - ~~~~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. export let e = fun3<"Hello" | "World">("Hello", "World"); // Assignment from the returned value should cause an error. diff --git a/tests/baselines/reference/typesWithSpecializedCallSignatures.types b/tests/baselines/reference/typesWithSpecializedCallSignatures.types index 113e24456f984..b587ee7840e27 100644 --- a/tests/baselines/reference/typesWithSpecializedCallSignatures.types +++ b/tests/baselines/reference/typesWithSpecializedCallSignatures.types @@ -125,7 +125,7 @@ var r1: Derived1 = c.foo('hi'); >c.foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >c : C >foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } ->'hi' : string +>'hi' : "hi" var r2: Derived2 = c.foo('bye'); >r2 : Derived2 @@ -134,7 +134,7 @@ var r2: Derived2 = c.foo('bye'); >c.foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >c : C >foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } ->'bye' : string +>'bye' : "bye" var r3: Base = c.foo('hm'); >r3 : Base diff --git a/tests/baselines/reference/typesWithSpecializedConstructSignatures.types b/tests/baselines/reference/typesWithSpecializedConstructSignatures.types index 2698439c527af..3036c0cea714d 100644 --- a/tests/baselines/reference/typesWithSpecializedConstructSignatures.types +++ b/tests/baselines/reference/typesWithSpecializedConstructSignatures.types @@ -100,14 +100,14 @@ var r1 = new C('hi'); >r1 : C >new C('hi') : C >C : typeof C ->'hi' : string +>'hi' : "hi" var r2: Derived2 = new i('bye'); >r2 : Derived2 >Derived2 : Derived2 >new i('bye') : Derived2 >i : I ->'bye' : string +>'bye' : "bye" var r3: Base = new a('hm'); >r3 : Base From 4b736da23172030a4a7970e06fa20cb094167535 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 16:30:22 -0700 Subject: [PATCH 17/38] Fixed issue in test. --- .../types/stringLiteral/stringLiteralTypesAndTuples01.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts index 7ac9aa3b42742..388f4567ed72a 100644 --- a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts @@ -4,7 +4,7 @@ let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; type RexOrRaptor = "t-rex" | "raptor" -let [im, a, dinosaur]: ["I'm", "a", Dinosaur] = ['I\'m', 'a', 't-rex']; +let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; rawr(dinosaur); From fd5dec4ff12aceb9be710624a5dc6d871e576e58 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 2 Oct 2015 16:34:15 -0700 Subject: [PATCH 18/38] Accepted baselines. --- .../stringLiteralTypesAndTuples01.errors.txt | 25 -------- .../stringLiteralTypesAndTuples01.js | 4 +- .../stringLiteralTypesAndTuples01.symbols | 41 +++++++++++++ .../stringLiteralTypesAndTuples01.types | 59 +++++++++++++++++++ 4 files changed, 102 insertions(+), 27 deletions(-) delete mode 100644 tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesAndTuples01.symbols create mode 100644 tests/baselines/reference/stringLiteralTypesAndTuples01.types diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt b/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt deleted file mode 100644 index 6a8704ce5546f..0000000000000 --- a/tests/baselines/reference/stringLiteralTypesAndTuples01.errors.txt +++ /dev/null @@ -1,25 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts(6,37): error TS2304: Cannot find name 'Dinosaur'. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts (1 errors) ==== - - // Should all be strings. - let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; - - type RexOrRaptor = "t-rex" | "raptor" - let [im, a, dinosaur]: ["I'm", "a", Dinosaur] = ['I\'m', 'a', 't-rex']; - ~~~~~~~~ -!!! error TS2304: Cannot find name 'Dinosaur'. - - rawr(dinosaur); - - function rawr(dino: RexOrRaptor) { - if (dino === "t-rex") { - return "ROAAAAR!"; - } - if (dino === "raptor") { - return "yip yip!"; - } - - throw "Unexpected " + dino; - } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.js b/tests/baselines/reference/stringLiteralTypesAndTuples01.js index 66e57b1eea5ac..ae02d12429a39 100644 --- a/tests/baselines/reference/stringLiteralTypesAndTuples01.js +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.js @@ -4,7 +4,7 @@ let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; type RexOrRaptor = "t-rex" | "raptor" -let [im, a, dinosaur]: ["I'm", "a", Dinosaur] = ['I\'m', 'a', 't-rex']; +let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; rawr(dinosaur); @@ -38,5 +38,5 @@ function rawr(dino) { //// [stringLiteralTypesAndTuples01.d.ts] declare let hello: string, brave: string, newish: string, world: string; declare type RexOrRaptor = "t-rex" | "raptor"; -declare let im: "I'm", a: "a", dinosaur: any; +declare let im: "I'm", a: "a", dinosaur: "t-rex" | "raptor"; declare function rawr(dino: RexOrRaptor): string; diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.symbols b/tests/baselines/reference/stringLiteralTypesAndTuples01.symbols new file mode 100644 index 0000000000000..b7ca53517ca15 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.symbols @@ -0,0 +1,41 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts === + +// Should all be strings. +let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; +>hello : Symbol(hello, Decl(stringLiteralTypesAndTuples01.ts, 2, 5)) +>brave : Symbol(brave, Decl(stringLiteralTypesAndTuples01.ts, 2, 11)) +>newish : Symbol(newish, Decl(stringLiteralTypesAndTuples01.ts, 2, 18)) +>world : Symbol(world, Decl(stringLiteralTypesAndTuples01.ts, 2, 26)) + +type RexOrRaptor = "t-rex" | "raptor" +>RexOrRaptor : Symbol(RexOrRaptor, Decl(stringLiteralTypesAndTuples01.ts, 2, 71)) + +let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; +>im : Symbol(im, Decl(stringLiteralTypesAndTuples01.ts, 5, 5)) +>a : Symbol(a, Decl(stringLiteralTypesAndTuples01.ts, 5, 8)) +>dinosaur : Symbol(dinosaur, Decl(stringLiteralTypesAndTuples01.ts, 5, 11)) +>RexOrRaptor : Symbol(RexOrRaptor, Decl(stringLiteralTypesAndTuples01.ts, 2, 71)) + +rawr(dinosaur); +>rawr : Symbol(rawr, Decl(stringLiteralTypesAndTuples01.ts, 7, 15)) +>dinosaur : Symbol(dinosaur, Decl(stringLiteralTypesAndTuples01.ts, 5, 11)) + +function rawr(dino: RexOrRaptor) { +>rawr : Symbol(rawr, Decl(stringLiteralTypesAndTuples01.ts, 7, 15)) +>dino : Symbol(dino, Decl(stringLiteralTypesAndTuples01.ts, 9, 14)) +>RexOrRaptor : Symbol(RexOrRaptor, Decl(stringLiteralTypesAndTuples01.ts, 2, 71)) + + if (dino === "t-rex") { +>dino : Symbol(dino, Decl(stringLiteralTypesAndTuples01.ts, 9, 14)) + + return "ROAAAAR!"; + } + if (dino === "raptor") { +>dino : Symbol(dino, Decl(stringLiteralTypesAndTuples01.ts, 9, 14)) + + return "yip yip!"; + } + + throw "Unexpected " + dino; +>dino : Symbol(dino, Decl(stringLiteralTypesAndTuples01.ts, 9, 14)) +} diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.types b/tests/baselines/reference/stringLiteralTypesAndTuples01.types new file mode 100644 index 0000000000000..740cf237412ca --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.types @@ -0,0 +1,59 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts === + +// Should all be strings. +let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; +>hello : string +>brave : string +>newish : string +>world : string +>["Hello", "Brave", "New", "World"] : [string, string, string, string] +>"Hello" : string +>"Brave" : string +>"New" : string +>"World" : string + +type RexOrRaptor = "t-rex" | "raptor" +>RexOrRaptor : "t-rex" | "raptor" + +let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; +>im : "I'm" +>a : "a" +>dinosaur : "t-rex" | "raptor" +>RexOrRaptor : "t-rex" | "raptor" +>['I\'m', 'a', 't-rex'] : ["I'm", "a", "t-rex" | "raptor"] +>'I\'m' : "I'm" +>'a' : "a" +>'t-rex' : "t-rex" | "raptor" + +rawr(dinosaur); +>rawr(dinosaur) : string +>rawr : (dino: "t-rex" | "raptor") => string +>dinosaur : "t-rex" | "raptor" + +function rawr(dino: RexOrRaptor) { +>rawr : (dino: "t-rex" | "raptor") => string +>dino : "t-rex" | "raptor" +>RexOrRaptor : "t-rex" | "raptor" + + if (dino === "t-rex") { +>dino === "t-rex" : boolean +>dino : "t-rex" | "raptor" +>"t-rex" : string + + return "ROAAAAR!"; +>"ROAAAAR!" : string + } + if (dino === "raptor") { +>dino === "raptor" : boolean +>dino : "t-rex" | "raptor" +>"raptor" : string + + return "yip yip!"; +>"yip yip!" : string + } + + throw "Unexpected " + dino; +>"Unexpected " + dino : string +>"Unexpected " : string +>dino : "t-rex" | "raptor" +} From f7a6ac7e0c0a03a3fd2d32a759e38b204f8cc5a5 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 7 Oct 2015 15:38:24 -0700 Subject: [PATCH 19/38] Added more tests. --- .../stringLiteralTypesOverloads03.ts | 46 +++++++++++++++++++ ...tringLiteralTypesWithVariousOperators01.ts | 29 ++++++++++++ ...tringLiteralTypesWithVariousOperators02.ts | 19 ++++++++ 3 files changed, 94 insertions(+) create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts new file mode 100644 index 0000000000000..a675c2c0eaed7 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts @@ -0,0 +1,46 @@ +// @declaration: true + +interface Base { + x: string; + y: number; +} + +interface HelloOrWorld extends Base { + p1: boolean; +} + +interface JustHello extends Base { + p2: boolean; +} + +interface JustWorld extends Base { + p3: boolean; +} + +let hello: "hello"; +let world: "world"; +let helloOrWorld: "hello" | "world"; + +function f(p: "hello"): JustHello; +function f(p: "hello" | "world"): HelloOrWorld; +function f(p: "world"): JustWorld; +function f(p: string): Base; +function f(...args: any[]): any { + return undefined; +} + +let fResult1 = f(hello); +let fResult2 = f(world); +let fResult3 = f(helloOrWorld); + +function g(p: string): Base; +function g(p: "hello"): JustHello; +function g(p: "hello" | "world"): HelloOrWorld; +function g(p: "world"): JustWorld; +function g(...args: any[]): any { + return undefined; +} + +let gResult1 = g(hello); +let gResult2 = g(world); +let gResult3 = g(helloOrWorld); \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts new file mode 100644 index 0000000000000..28c34d3c74a99 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts @@ -0,0 +1,29 @@ +// @declaration: true + +let abc: "ABC" = "ABC"; +let xyz: "XYZ" = "XYZ"; +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + +let a = "" + abc; +let b = abc + ""; +let c = 10 + abc; +let d = abc + 10; +let e = xyz + abc; +let f = abc + xyz; +let g = true + abc; +let h = abc + true; +let i = abc + abcOrXyz + xyz; +let j = abcOrXyz + abcOrXyz; +let k = +abcOrXyz; +let l = -abcOrXyz; +let m = abcOrXyzOrNumber + ""; +let n = "" + abcOrXyzOrNumber; +let o = abcOrXyzOrNumber + abcOrXyz; +let p = abcOrXyz + abcOrXyzOrNumber; +let q = !abcOrXyzOrNumber; +let r = ~abcOrXyzOrNumber; +let s = abcOrXyzOrNumber < abcOrXyzOrNumber; +let t = abcOrXyzOrNumber >= abcOrXyz; +let u = abc === abcOrXyz; +let v = abcOrXyz === abcOrXyzOrNumber; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts new file mode 100644 index 0000000000000..cf66f66e47c30 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts @@ -0,0 +1,19 @@ +// @declaration: true + +let abc: "ABC" = "ABC"; +let xyz: "XYZ" = "XYZ"; +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + +let a = abcOrXyzOrNumber + 100; +let b = 100 + abcOrXyzOrNumber; +let c = abcOrXyzOrNumber + abcOrXyzOrNumber; +let d = abcOrXyzOrNumber + true; +let e = false + abcOrXyzOrNumber; +let f = abcOrXyzOrNumber++; +let g = --abcOrXyzOrNumber; +let h = abcOrXyzOrNumber ^ 10; +let i = abcOrXyzOrNumber | 10; +let j = abc < xyz; +let k = abc === xyz; +let l = abc != xyz; \ No newline at end of file From a440f06cac2f5265f321cf06ea147310d759afb6 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 8 Oct 2015 14:10:15 -0700 Subject: [PATCH 20/38] Accepted baselines. --- .../stringLiteralTypesOverloads03.js | 104 ++++++++++++ .../stringLiteralTypesOverloads03.symbols | 131 +++++++++++++++ .../stringLiteralTypesOverloads03.types | 137 ++++++++++++++++ ...tringLiteralTypesWithVariousOperators01.js | 86 ++++++++++ ...LiteralTypesWithVariousOperators01.symbols | 116 +++++++++++++ ...ngLiteralTypesWithVariousOperators01.types | 152 ++++++++++++++++++ ...eralTypesWithVariousOperators02.errors.txt | 57 +++++++ ...tringLiteralTypesWithVariousOperators02.js | 56 +++++++ 8 files changed, 839 insertions(+) create mode 100644 tests/baselines/reference/stringLiteralTypesOverloads03.js create mode 100644 tests/baselines/reference/stringLiteralTypesOverloads03.symbols create mode 100644 tests/baselines/reference/stringLiteralTypesOverloads03.types create mode 100644 tests/baselines/reference/stringLiteralTypesWithVariousOperators01.js create mode 100644 tests/baselines/reference/stringLiteralTypesWithVariousOperators01.symbols create mode 100644 tests/baselines/reference/stringLiteralTypesWithVariousOperators01.types create mode 100644 tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesWithVariousOperators02.js diff --git a/tests/baselines/reference/stringLiteralTypesOverloads03.js b/tests/baselines/reference/stringLiteralTypesOverloads03.js new file mode 100644 index 0000000000000..c1e7916088095 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads03.js @@ -0,0 +1,104 @@ +//// [stringLiteralTypesOverloads03.ts] + +interface Base { + x: string; + y: number; +} + +interface HelloOrWorld extends Base { + p1: boolean; +} + +interface JustHello extends Base { + p2: boolean; +} + +interface JustWorld extends Base { + p3: boolean; +} + +let hello: "hello"; +let world: "world"; +let helloOrWorld: "hello" | "world"; + +function f(p: "hello"): JustHello; +function f(p: "hello" | "world"): HelloOrWorld; +function f(p: "world"): JustWorld; +function f(p: string): Base; +function f(...args: any[]): any { + return undefined; +} + +let fResult1 = f(hello); +let fResult2 = f(world); +let fResult3 = f(helloOrWorld); + +function g(p: string): Base; +function g(p: "hello"): JustHello; +function g(p: "hello" | "world"): HelloOrWorld; +function g(p: "world"): JustWorld; +function g(...args: any[]): any { + return undefined; +} + +let gResult1 = g(hello); +let gResult2 = g(world); +let gResult3 = g(helloOrWorld); + +//// [stringLiteralTypesOverloads03.js] +var hello; +var world; +var helloOrWorld; +function f() { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i - 0] = arguments[_i]; + } + return undefined; +} +var fResult1 = f(hello); +var fResult2 = f(world); +var fResult3 = f(helloOrWorld); +function g() { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i - 0] = arguments[_i]; + } + return undefined; +} +var gResult1 = g(hello); +var gResult2 = g(world); +var gResult3 = g(helloOrWorld); + + +//// [stringLiteralTypesOverloads03.d.ts] +interface Base { + x: string; + y: number; +} +interface HelloOrWorld extends Base { + p1: boolean; +} +interface JustHello extends Base { + p2: boolean; +} +interface JustWorld extends Base { + p3: boolean; +} +declare let hello: "hello"; +declare let world: "world"; +declare let helloOrWorld: "hello" | "world"; +declare function f(p: "hello"): JustHello; +declare function f(p: "hello" | "world"): HelloOrWorld; +declare function f(p: "world"): JustWorld; +declare function f(p: string): Base; +declare let fResult1: JustHello; +declare let fResult2: JustWorld; +declare let fResult3: HelloOrWorld; +declare function g(p: string): Base; +declare function g(p: "hello"): JustHello; +declare function g(p: "hello" | "world"): HelloOrWorld; +declare function g(p: "world"): JustWorld; +declare let gResult1: JustHello; +declare let gResult2: JustWorld; +declare let gResult3: Base; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads03.symbols b/tests/baselines/reference/stringLiteralTypesOverloads03.symbols new file mode 100644 index 0000000000000..a0a48de790f93 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads03.symbols @@ -0,0 +1,131 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts === + +interface Base { +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + + x: string; +>x : Symbol(x, Decl(stringLiteralTypesOverloads03.ts, 1, 16)) + + y: number; +>y : Symbol(y, Decl(stringLiteralTypesOverloads03.ts, 2, 14)) +} + +interface HelloOrWorld extends Base { +>HelloOrWorld : Symbol(HelloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 4, 1)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + + p1: boolean; +>p1 : Symbol(p1, Decl(stringLiteralTypesOverloads03.ts, 6, 37)) +} + +interface JustHello extends Base { +>JustHello : Symbol(JustHello, Decl(stringLiteralTypesOverloads03.ts, 8, 1)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + + p2: boolean; +>p2 : Symbol(p2, Decl(stringLiteralTypesOverloads03.ts, 10, 34)) +} + +interface JustWorld extends Base { +>JustWorld : Symbol(JustWorld, Decl(stringLiteralTypesOverloads03.ts, 12, 1)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + + p3: boolean; +>p3 : Symbol(p3, Decl(stringLiteralTypesOverloads03.ts, 14, 34)) +} + +let hello: "hello"; +>hello : Symbol(hello, Decl(stringLiteralTypesOverloads03.ts, 18, 3)) + +let world: "world"; +>world : Symbol(world, Decl(stringLiteralTypesOverloads03.ts, 19, 3)) + +let helloOrWorld: "hello" | "world"; +>helloOrWorld : Symbol(helloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 20, 3)) + +function f(p: "hello"): JustHello; +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 22, 11)) +>JustHello : Symbol(JustHello, Decl(stringLiteralTypesOverloads03.ts, 8, 1)) + +function f(p: "hello" | "world"): HelloOrWorld; +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 23, 11)) +>HelloOrWorld : Symbol(HelloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 4, 1)) + +function f(p: "world"): JustWorld; +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 24, 11)) +>JustWorld : Symbol(JustWorld, Decl(stringLiteralTypesOverloads03.ts, 12, 1)) + +function f(p: string): Base; +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 25, 11)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + +function f(...args: any[]): any { +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>args : Symbol(args, Decl(stringLiteralTypesOverloads03.ts, 26, 11)) + + return undefined; +>undefined : Symbol(undefined) +} + +let fResult1 = f(hello); +>fResult1 : Symbol(fResult1, Decl(stringLiteralTypesOverloads03.ts, 30, 3)) +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>hello : Symbol(hello, Decl(stringLiteralTypesOverloads03.ts, 18, 3)) + +let fResult2 = f(world); +>fResult2 : Symbol(fResult2, Decl(stringLiteralTypesOverloads03.ts, 31, 3)) +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>world : Symbol(world, Decl(stringLiteralTypesOverloads03.ts, 19, 3)) + +let fResult3 = f(helloOrWorld); +>fResult3 : Symbol(fResult3, Decl(stringLiteralTypesOverloads03.ts, 32, 3)) +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>helloOrWorld : Symbol(helloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 20, 3)) + +function g(p: string): Base; +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 34, 11)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + +function g(p: "hello"): JustHello; +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 35, 11)) +>JustHello : Symbol(JustHello, Decl(stringLiteralTypesOverloads03.ts, 8, 1)) + +function g(p: "hello" | "world"): HelloOrWorld; +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 36, 11)) +>HelloOrWorld : Symbol(HelloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 4, 1)) + +function g(p: "world"): JustWorld; +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 37, 11)) +>JustWorld : Symbol(JustWorld, Decl(stringLiteralTypesOverloads03.ts, 12, 1)) + +function g(...args: any[]): any { +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>args : Symbol(args, Decl(stringLiteralTypesOverloads03.ts, 38, 11)) + + return undefined; +>undefined : Symbol(undefined) +} + +let gResult1 = g(hello); +>gResult1 : Symbol(gResult1, Decl(stringLiteralTypesOverloads03.ts, 42, 3)) +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>hello : Symbol(hello, Decl(stringLiteralTypesOverloads03.ts, 18, 3)) + +let gResult2 = g(world); +>gResult2 : Symbol(gResult2, Decl(stringLiteralTypesOverloads03.ts, 43, 3)) +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>world : Symbol(world, Decl(stringLiteralTypesOverloads03.ts, 19, 3)) + +let gResult3 = g(helloOrWorld); +>gResult3 : Symbol(gResult3, Decl(stringLiteralTypesOverloads03.ts, 44, 3)) +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>helloOrWorld : Symbol(helloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 20, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesOverloads03.types b/tests/baselines/reference/stringLiteralTypesOverloads03.types new file mode 100644 index 0000000000000..643979ee987ca --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads03.types @@ -0,0 +1,137 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts === + +interface Base { +>Base : Base + + x: string; +>x : string + + y: number; +>y : number +} + +interface HelloOrWorld extends Base { +>HelloOrWorld : HelloOrWorld +>Base : Base + + p1: boolean; +>p1 : boolean +} + +interface JustHello extends Base { +>JustHello : JustHello +>Base : Base + + p2: boolean; +>p2 : boolean +} + +interface JustWorld extends Base { +>JustWorld : JustWorld +>Base : Base + + p3: boolean; +>p3 : boolean +} + +let hello: "hello"; +>hello : "hello" + +let world: "world"; +>world : "world" + +let helloOrWorld: "hello" | "world"; +>helloOrWorld : "hello" | "world" + +function f(p: "hello"): JustHello; +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>p : "hello" +>JustHello : JustHello + +function f(p: "hello" | "world"): HelloOrWorld; +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>p : "hello" | "world" +>HelloOrWorld : HelloOrWorld + +function f(p: "world"): JustWorld; +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>p : "world" +>JustWorld : JustWorld + +function f(p: string): Base; +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>p : string +>Base : Base + +function f(...args: any[]): any { +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>args : any[] + + return undefined; +>undefined : undefined +} + +let fResult1 = f(hello); +>fResult1 : JustHello +>f(hello) : JustHello +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>hello : "hello" + +let fResult2 = f(world); +>fResult2 : JustWorld +>f(world) : JustWorld +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>world : "world" + +let fResult3 = f(helloOrWorld); +>fResult3 : HelloOrWorld +>f(helloOrWorld) : HelloOrWorld +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>helloOrWorld : "hello" | "world" + +function g(p: string): Base; +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>p : string +>Base : Base + +function g(p: "hello"): JustHello; +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>p : "hello" +>JustHello : JustHello + +function g(p: "hello" | "world"): HelloOrWorld; +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>p : "hello" | "world" +>HelloOrWorld : HelloOrWorld + +function g(p: "world"): JustWorld; +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>p : "world" +>JustWorld : JustWorld + +function g(...args: any[]): any { +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>args : any[] + + return undefined; +>undefined : undefined +} + +let gResult1 = g(hello); +>gResult1 : JustHello +>g(hello) : JustHello +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>hello : "hello" + +let gResult2 = g(world); +>gResult2 : JustWorld +>g(world) : JustWorld +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>world : "world" + +let gResult3 = g(helloOrWorld); +>gResult3 : Base +>g(helloOrWorld) : Base +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>helloOrWorld : "hello" | "world" + diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.js b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.js new file mode 100644 index 0000000000000..1576540e24446 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.js @@ -0,0 +1,86 @@ +//// [stringLiteralTypesWithVariousOperators01.ts] + +let abc: "ABC" = "ABC"; +let xyz: "XYZ" = "XYZ"; +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + +let a = "" + abc; +let b = abc + ""; +let c = 10 + abc; +let d = abc + 10; +let e = xyz + abc; +let f = abc + xyz; +let g = true + abc; +let h = abc + true; +let i = abc + abcOrXyz + xyz; +let j = abcOrXyz + abcOrXyz; +let k = +abcOrXyz; +let l = -abcOrXyz; +let m = abcOrXyzOrNumber + ""; +let n = "" + abcOrXyzOrNumber; +let o = abcOrXyzOrNumber + abcOrXyz; +let p = abcOrXyz + abcOrXyzOrNumber; +let q = !abcOrXyzOrNumber; +let r = ~abcOrXyzOrNumber; +let s = abcOrXyzOrNumber < abcOrXyzOrNumber; +let t = abcOrXyzOrNumber >= abcOrXyz; +let u = abc === abcOrXyz; +let v = abcOrXyz === abcOrXyzOrNumber; + +//// [stringLiteralTypesWithVariousOperators01.js] +var abc = "ABC"; +var xyz = "XYZ"; +var abcOrXyz = abc || xyz; +var abcOrXyzOrNumber = abcOrXyz || 100; +var a = "" + abc; +var b = abc + ""; +var c = 10 + abc; +var d = abc + 10; +var e = xyz + abc; +var f = abc + xyz; +var g = true + abc; +var h = abc + true; +var i = abc + abcOrXyz + xyz; +var j = abcOrXyz + abcOrXyz; +var k = +abcOrXyz; +var l = -abcOrXyz; +var m = abcOrXyzOrNumber + ""; +var n = "" + abcOrXyzOrNumber; +var o = abcOrXyzOrNumber + abcOrXyz; +var p = abcOrXyz + abcOrXyzOrNumber; +var q = !abcOrXyzOrNumber; +var r = ~abcOrXyzOrNumber; +var s = abcOrXyzOrNumber < abcOrXyzOrNumber; +var t = abcOrXyzOrNumber >= abcOrXyz; +var u = abc === abcOrXyz; +var v = abcOrXyz === abcOrXyzOrNumber; + + +//// [stringLiteralTypesWithVariousOperators01.d.ts] +declare let abc: "ABC"; +declare let xyz: "XYZ"; +declare let abcOrXyz: "ABC" | "XYZ"; +declare let abcOrXyzOrNumber: "ABC" | "XYZ" | number; +declare let a: string; +declare let b: string; +declare let c: string; +declare let d: string; +declare let e: string; +declare let f: string; +declare let g: string; +declare let h: string; +declare let i: string; +declare let j: string; +declare let k: number; +declare let l: number; +declare let m: string; +declare let n: string; +declare let o: string; +declare let p: string; +declare let q: boolean; +declare let r: number; +declare let s: boolean; +declare let t: boolean; +declare let u: boolean; +declare let v: boolean; diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.symbols b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.symbols new file mode 100644 index 0000000000000..cc9c3aa842bd3 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.symbols @@ -0,0 +1,116 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts === + +let abc: "ABC" = "ABC"; +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let xyz: "XYZ" = "XYZ"; +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) + +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) + +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let a = "" + abc; +>a : Symbol(a, Decl(stringLiteralTypesWithVariousOperators01.ts, 6, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let b = abc + ""; +>b : Symbol(b, Decl(stringLiteralTypesWithVariousOperators01.ts, 7, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let c = 10 + abc; +>c : Symbol(c, Decl(stringLiteralTypesWithVariousOperators01.ts, 8, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let d = abc + 10; +>d : Symbol(d, Decl(stringLiteralTypesWithVariousOperators01.ts, 9, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let e = xyz + abc; +>e : Symbol(e, Decl(stringLiteralTypesWithVariousOperators01.ts, 10, 3)) +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let f = abc + xyz; +>f : Symbol(f, Decl(stringLiteralTypesWithVariousOperators01.ts, 11, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) + +let g = true + abc; +>g : Symbol(g, Decl(stringLiteralTypesWithVariousOperators01.ts, 12, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let h = abc + true; +>h : Symbol(h, Decl(stringLiteralTypesWithVariousOperators01.ts, 13, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let i = abc + abcOrXyz + xyz; +>i : Symbol(i, Decl(stringLiteralTypesWithVariousOperators01.ts, 14, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) + +let j = abcOrXyz + abcOrXyz; +>j : Symbol(j, Decl(stringLiteralTypesWithVariousOperators01.ts, 15, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let k = +abcOrXyz; +>k : Symbol(k, Decl(stringLiteralTypesWithVariousOperators01.ts, 16, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let l = -abcOrXyz; +>l : Symbol(l, Decl(stringLiteralTypesWithVariousOperators01.ts, 17, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let m = abcOrXyzOrNumber + ""; +>m : Symbol(m, Decl(stringLiteralTypesWithVariousOperators01.ts, 18, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let n = "" + abcOrXyzOrNumber; +>n : Symbol(n, Decl(stringLiteralTypesWithVariousOperators01.ts, 19, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let o = abcOrXyzOrNumber + abcOrXyz; +>o : Symbol(o, Decl(stringLiteralTypesWithVariousOperators01.ts, 20, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let p = abcOrXyz + abcOrXyzOrNumber; +>p : Symbol(p, Decl(stringLiteralTypesWithVariousOperators01.ts, 21, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let q = !abcOrXyzOrNumber; +>q : Symbol(q, Decl(stringLiteralTypesWithVariousOperators01.ts, 22, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let r = ~abcOrXyzOrNumber; +>r : Symbol(r, Decl(stringLiteralTypesWithVariousOperators01.ts, 23, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let s = abcOrXyzOrNumber < abcOrXyzOrNumber; +>s : Symbol(s, Decl(stringLiteralTypesWithVariousOperators01.ts, 24, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let t = abcOrXyzOrNumber >= abcOrXyz; +>t : Symbol(t, Decl(stringLiteralTypesWithVariousOperators01.ts, 25, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let u = abc === abcOrXyz; +>u : Symbol(u, Decl(stringLiteralTypesWithVariousOperators01.ts, 26, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let v = abcOrXyz === abcOrXyzOrNumber; +>v : Symbol(v, Decl(stringLiteralTypesWithVariousOperators01.ts, 27, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.types b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.types new file mode 100644 index 0000000000000..90cb538b800d1 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.types @@ -0,0 +1,152 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts === + +let abc: "ABC" = "ABC"; +>abc : "ABC" +>"ABC" : "ABC" + +let xyz: "XYZ" = "XYZ"; +>xyz : "XYZ" +>"XYZ" : "XYZ" + +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +>abcOrXyz : "ABC" | "XYZ" +>abc || xyz : "ABC" | "XYZ" +>abc : "ABC" +>xyz : "XYZ" + +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>abcOrXyz || 100 : "ABC" | "XYZ" | number +>abcOrXyz : "ABC" | "XYZ" +>100 : number + +let a = "" + abc; +>a : string +>"" + abc : string +>"" : string +>abc : "ABC" + +let b = abc + ""; +>b : string +>abc + "" : string +>abc : "ABC" +>"" : string + +let c = 10 + abc; +>c : string +>10 + abc : string +>10 : number +>abc : "ABC" + +let d = abc + 10; +>d : string +>abc + 10 : string +>abc : "ABC" +>10 : number + +let e = xyz + abc; +>e : string +>xyz + abc : string +>xyz : "XYZ" +>abc : "ABC" + +let f = abc + xyz; +>f : string +>abc + xyz : string +>abc : "ABC" +>xyz : "XYZ" + +let g = true + abc; +>g : string +>true + abc : string +>true : boolean +>abc : "ABC" + +let h = abc + true; +>h : string +>abc + true : string +>abc : "ABC" +>true : boolean + +let i = abc + abcOrXyz + xyz; +>i : string +>abc + abcOrXyz + xyz : string +>abc + abcOrXyz : string +>abc : "ABC" +>abcOrXyz : "ABC" | "XYZ" +>xyz : "XYZ" + +let j = abcOrXyz + abcOrXyz; +>j : string +>abcOrXyz + abcOrXyz : string +>abcOrXyz : "ABC" | "XYZ" +>abcOrXyz : "ABC" | "XYZ" + +let k = +abcOrXyz; +>k : number +>+abcOrXyz : number +>abcOrXyz : "ABC" | "XYZ" + +let l = -abcOrXyz; +>l : number +>-abcOrXyz : number +>abcOrXyz : "ABC" | "XYZ" + +let m = abcOrXyzOrNumber + ""; +>m : string +>abcOrXyzOrNumber + "" : string +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>"" : string + +let n = "" + abcOrXyzOrNumber; +>n : string +>"" + abcOrXyzOrNumber : string +>"" : string +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let o = abcOrXyzOrNumber + abcOrXyz; +>o : string +>abcOrXyzOrNumber + abcOrXyz : string +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>abcOrXyz : "ABC" | "XYZ" + +let p = abcOrXyz + abcOrXyzOrNumber; +>p : string +>abcOrXyz + abcOrXyzOrNumber : string +>abcOrXyz : "ABC" | "XYZ" +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let q = !abcOrXyzOrNumber; +>q : boolean +>!abcOrXyzOrNumber : boolean +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let r = ~abcOrXyzOrNumber; +>r : number +>~abcOrXyzOrNumber : number +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let s = abcOrXyzOrNumber < abcOrXyzOrNumber; +>s : boolean +>abcOrXyzOrNumber < abcOrXyzOrNumber : boolean +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let t = abcOrXyzOrNumber >= abcOrXyz; +>t : boolean +>abcOrXyzOrNumber >= abcOrXyz : boolean +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>abcOrXyz : "ABC" | "XYZ" + +let u = abc === abcOrXyz; +>u : boolean +>abc === abcOrXyz : boolean +>abc : "ABC" +>abcOrXyz : "ABC" | "XYZ" + +let v = abcOrXyz === abcOrXyzOrNumber; +>v : boolean +>abcOrXyz === abcOrXyzOrNumber : boolean +>abcOrXyz : "ABC" | "XYZ" +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt new file mode 100644 index 0000000000000..92afe67df0c62 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt @@ -0,0 +1,57 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(7,9): error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and 'number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(8,9): error TS2365: Operator '+' cannot be applied to types 'number' and '"ABC" | "XYZ" | number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(9,9): error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and '"ABC" | "XYZ" | number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(10,9): error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and 'boolean'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(11,9): error TS2365: Operator '+' cannot be applied to types 'boolean' and '"ABC" | "XYZ" | number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(12,9): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(13,11): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(14,9): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(15,9): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(16,9): error TS2365: Operator '<' cannot be applied to types '"ABC"' and '"XYZ"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(17,9): error TS2365: Operator '===' cannot be applied to types '"ABC"' and '"XYZ"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(18,9): error TS2365: Operator '!=' cannot be applied to types '"ABC"' and '"XYZ"'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts (12 errors) ==== + + let abc: "ABC" = "ABC"; + let xyz: "XYZ" = "XYZ"; + let abcOrXyz: "ABC" | "XYZ" = abc || xyz; + let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + + let a = abcOrXyzOrNumber + 100; + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and 'number'. + let b = 100 + abcOrXyzOrNumber; + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types 'number' and '"ABC" | "XYZ" | number'. + let c = abcOrXyzOrNumber + abcOrXyzOrNumber; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and '"ABC" | "XYZ" | number'. + let d = abcOrXyzOrNumber + true; + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and 'boolean'. + let e = false + abcOrXyzOrNumber; + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types 'boolean' and '"ABC" | "XYZ" | number'. + let f = abcOrXyzOrNumber++; + ~~~~~~~~~~~~~~~~ +!!! error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. + let g = --abcOrXyzOrNumber; + ~~~~~~~~~~~~~~~~ +!!! error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. + let h = abcOrXyzOrNumber ^ 10; + ~~~~~~~~~~~~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + let i = abcOrXyzOrNumber | 10; + ~~~~~~~~~~~~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + let j = abc < xyz; + ~~~~~~~~~ +!!! error TS2365: Operator '<' cannot be applied to types '"ABC"' and '"XYZ"'. + let k = abc === xyz; + ~~~~~~~~~~~ +!!! error TS2365: Operator '===' cannot be applied to types '"ABC"' and '"XYZ"'. + let l = abc != xyz; + ~~~~~~~~~~ +!!! error TS2365: Operator '!=' cannot be applied to types '"ABC"' and '"XYZ"'. \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.js b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.js new file mode 100644 index 0000000000000..4914ecf3d178a --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.js @@ -0,0 +1,56 @@ +//// [stringLiteralTypesWithVariousOperators02.ts] + +let abc: "ABC" = "ABC"; +let xyz: "XYZ" = "XYZ"; +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + +let a = abcOrXyzOrNumber + 100; +let b = 100 + abcOrXyzOrNumber; +let c = abcOrXyzOrNumber + abcOrXyzOrNumber; +let d = abcOrXyzOrNumber + true; +let e = false + abcOrXyzOrNumber; +let f = abcOrXyzOrNumber++; +let g = --abcOrXyzOrNumber; +let h = abcOrXyzOrNumber ^ 10; +let i = abcOrXyzOrNumber | 10; +let j = abc < xyz; +let k = abc === xyz; +let l = abc != xyz; + +//// [stringLiteralTypesWithVariousOperators02.js] +var abc = "ABC"; +var xyz = "XYZ"; +var abcOrXyz = abc || xyz; +var abcOrXyzOrNumber = abcOrXyz || 100; +var a = abcOrXyzOrNumber + 100; +var b = 100 + abcOrXyzOrNumber; +var c = abcOrXyzOrNumber + abcOrXyzOrNumber; +var d = abcOrXyzOrNumber + true; +var e = false + abcOrXyzOrNumber; +var f = abcOrXyzOrNumber++; +var g = --abcOrXyzOrNumber; +var h = abcOrXyzOrNumber ^ 10; +var i = abcOrXyzOrNumber | 10; +var j = abc < xyz; +var k = abc === xyz; +var l = abc != xyz; + + +//// [stringLiteralTypesWithVariousOperators02.d.ts] +declare let abc: "ABC"; +declare let xyz: "XYZ"; +declare let abcOrXyz: "ABC" | "XYZ"; +declare let abcOrXyzOrNumber: "ABC" | "XYZ" | number; +declare let a: any; +declare let b: any; +declare let c: any; +declare let d: any; +declare let e: any; +declare let f: number; +declare let g: number; +declare let h: number; +declare let i: number; +declare let j: boolean; +declare let k: boolean; +declare let l: boolean; From d2e2a55a9ed8e212c1c196a33c9163f8c8bf7da8 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 8 Oct 2015 14:36:36 -0700 Subject: [PATCH 21/38] Added fourslash test. --- ...ickInfoDisplayPartsVarWithStringTypes01.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/cases/fourslash/quickInfoDisplayPartsVarWithStringTypes01.ts diff --git a/tests/cases/fourslash/quickInfoDisplayPartsVarWithStringTypes01.ts b/tests/cases/fourslash/quickInfoDisplayPartsVarWithStringTypes01.ts new file mode 100644 index 0000000000000..b48c7cfa3de19 --- /dev/null +++ b/tests/cases/fourslash/quickInfoDisplayPartsVarWithStringTypes01.ts @@ -0,0 +1,41 @@ +/// + +////let /*1*/hello: "hello" | 'hello' = "hello"; +////let /*2*/world: 'world' = "world"; +////let /*3*/helloOrWorld: "hello" | 'world'; + +goTo.marker("1"); +verify.verifyQuickInfoDisplayParts("let", "", { start: test.markerByName('1').position, length: "hello".length }, [ + { text: "let", kind: "keyword" }, + { text: " ", kind: "space" }, + { text: "hello", kind: "localName" }, + { text: ":", kind: "punctuation" }, + { text: " ", kind: "space" }, + { text: '"hello"', kind: "stringLiteral" }, ], + /*documentation*/ []); + +goTo.marker("2"); +verify.verifyQuickInfoDisplayParts("let", "", { start: test.markerByName('2').position, length: "world".length }, [ + { text: "let", kind: "keyword" }, + { text: " ", kind: "space" }, + { text: "world", kind: "localName" }, + { text: ":", kind: "punctuation" }, + { text: " ", kind: "space" }, + { text: '"world"', kind: "stringLiteral" }, + ], + /*documentation*/[]); + +goTo.marker("3"); +verify.verifyQuickInfoDisplayParts("let", "", { start: test.markerByName('3').position, length: "helloOrWorld".length }, [ + { text: "let", kind: "keyword" }, + { text: " ", kind: "space" }, + { text: "helloOrWorld", kind: "localName" }, + { text: ":", kind: "punctuation" }, + { text: " ", kind: "space" }, + { text: '"hello"', kind: "stringLiteral" }, + { text: " ", kind: "space" }, + { text: "|", kind: "punctuation" }, + { text: " ", kind: "space" }, + { text: '"world"', kind: "stringLiteral" }, + ], + /*documentation*/[]); \ No newline at end of file From 74ac57ddfcdca56e037ca7019f06b288560df43a Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 8 Oct 2015 15:26:00 -0700 Subject: [PATCH 22/38] Accepted post-merge baselines. --- .../parserErrorRecovery_IncompleteMemberVariable1.symbols | 6 +++--- .../parserErrorRecovery_IncompleteMemberVariable1.types | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols index db9cefbf64291..7b8be05f923f8 100644 --- a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols +++ b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols @@ -27,9 +27,9 @@ module Shapes { // Instance member getDist() { return Math.sqrt(this.x * this.x + this.y * this.y); } >getDist : Symbol(getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 60)) ->Math.sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, 620, 27)) ->Math : Symbol(Math, Decl(lib.d.ts, 522, 1), Decl(lib.d.ts, 633, 11)) ->sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, 620, 27)) +>Math.sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, --, --)) >this.x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) >this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) >x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types index bb51151cfd71b..b38bbbfe64d4d 100644 --- a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types +++ b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types @@ -34,17 +34,17 @@ module Shapes { >this.x * this.x + this.y * this.y : number >this.x * this.x : number >this.x : number ->this : Point +>this : this >x : number >this.x : number ->this : Point +>this : this >x : number >this.y * this.y : number >this.y : number ->this : Point +>this : this >y : number >this.y : number ->this : Point +>this : this >y : number // Static member From 61ece765c74f7cf48a7b61c9137405e206bd1766 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 8 Oct 2015 15:27:12 -0700 Subject: [PATCH 23/38] Return the string literal type itself instead of the union type. --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bf8072bf2a5fd..3b43533e56bd3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10292,7 +10292,7 @@ namespace ts { if (contextualType.flags & TypeFlags.Union) { for (const type of (contextualType).types) { if (type.flags & TypeFlags.StringLiteral && (type).text === node.text) { - return contextualType; + return type; } } } From 84b64c4c670676b06cbf98891c85ab8c9f537182 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 8 Oct 2015 15:34:46 -0700 Subject: [PATCH 24/38] Accepted baselines. --- tests/baselines/reference/stringLiteralTypesAndTuples01.types | 4 ++-- .../reference/stringLiteralTypesInUnionTypes01.types | 4 ++-- .../reference/stringLiteralTypesInUnionTypes02.types | 4 ++-- .../reference/stringLiteralTypesInUnionTypes04.types | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.types b/tests/baselines/reference/stringLiteralTypesAndTuples01.types index 740cf237412ca..c874973e3e590 100644 --- a/tests/baselines/reference/stringLiteralTypesAndTuples01.types +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.types @@ -20,10 +20,10 @@ let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; >a : "a" >dinosaur : "t-rex" | "raptor" >RexOrRaptor : "t-rex" | "raptor" ->['I\'m', 'a', 't-rex'] : ["I'm", "a", "t-rex" | "raptor"] +>['I\'m', 'a', 't-rex'] : ["I'm", "a", "t-rex"] >'I\'m' : "I'm" >'a' : "a" ->'t-rex' : "t-rex" | "raptor" +>'t-rex' : "t-rex" rawr(dinosaur); >rawr(dinosaur) : string diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types index b5a2d876bcc11..e5ff509822b99 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types @@ -5,12 +5,12 @@ type T = "foo" | "bar" | "baz"; var x: "foo" | "bar" | "baz" = "foo"; >x : "foo" | "bar" | "baz" ->"foo" : "foo" | "bar" | "baz" +>"foo" : "foo" var y: T = "bar"; >y : "foo" | "bar" | "baz" >T : "foo" | "bar" | "baz" ->"bar" : "foo" | "bar" | "baz" +>"bar" : "bar" if (x === "foo") { >x === "foo" : boolean diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types index e3ac5ba481785..b468c620376ba 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types @@ -5,12 +5,12 @@ type T = string | "foo" | "bar" | "baz"; var x: "foo" | "bar" | "baz" | string = "foo"; >x : "foo" | "bar" | "baz" | string ->"foo" : "foo" | "bar" | "baz" | string +>"foo" : "foo" var y: T = "bar"; >y : string | "foo" | "bar" | "baz" >T : string | "foo" | "bar" | "baz" ->"bar" : string | "foo" | "bar" | "baz" +>"bar" : "bar" if (x === "foo") { >x === "foo" : boolean diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types index 9a010b490cca2..ad92dc360d0a9 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types @@ -6,12 +6,12 @@ type T = "" | "foo"; let x: T = ""; >x : "" | "foo" >T : "" | "foo" ->"" : "" | "foo" +>"" : "" let y: T = "foo"; >y : "" | "foo" >T : "" | "foo" ->"foo" : "" | "foo" +>"foo" : "foo" if (x === "") { >x === "" : boolean From 3788254fdc23e51f90d00444e02062671d89871d Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 8 Oct 2015 15:49:32 -0700 Subject: [PATCH 25/38] Semicolon. --- src/compiler/parser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 5726d260876e8..dcc924d29b451 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2366,7 +2366,7 @@ namespace ts { let node = tryParse(parseKeywordAndNoDot); return node || parseTypeReferenceOrTypePredicate(); case SyntaxKind.StringLiteral: - return parseLiteralNode(/*internName*/ true) + return parseLiteralNode(/*internName*/ true); case SyntaxKind.VoidKeyword: case SyntaxKind.ThisKeyword: return parseTokenNode(); From ebc47d5e0213d31c7eee6111b7c9f09078c8f1ae Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 8 Oct 2015 16:04:09 -0700 Subject: [PATCH 26/38] Linting. --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3b43533e56bd3..be8a486cf6558 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10286,7 +10286,7 @@ namespace ts { function checkStringLiteralExpression(node: LiteralExpression) { // TODO (drosen): Do we want to apply the same approach to no-sub template literals? - + let contextualType = getContextualType(node); if (contextualType) { if (contextualType.flags & TypeFlags.Union) { @@ -10435,7 +10435,7 @@ namespace ts { case SyntaxKind.StringLiteral: return checkStringLiteralExpression(node); case SyntaxKind.NoSubstitutionTemplateLiteral: - return stringType + return stringType; case SyntaxKind.RegularExpressionLiteral: return globalRegExpType; case SyntaxKind.ArrayLiteralExpression: From ec0d49a312171c408263c40ea777963b4d02963f Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 15 Oct 2015 14:26:27 -0700 Subject: [PATCH 27/38] Always use a string literal type if contextually typed by any string literal types. --- src/compiler/checker.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2980d8a7cad94..3b7436f01e21a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5691,6 +5691,10 @@ namespace ts { return !!getPropertyOfType(type, "0"); } + function isStringLiteralType(type: Type) { + return type.flags & TypeFlags.StringLiteral; + } + /** * Check if a Type was written as a tuple type literal. * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. @@ -6953,6 +6957,10 @@ namespace ts { return applyToContextualType(type, t => getIndexTypeOfStructuredType(t, kind)); } + function contextualTypeIsStringLiteralType(type: Type): boolean { + return !!(type.flags & TypeFlags.Union ? forEach((type).types, isStringLiteralType) : isStringLiteralType(type)); + } + // Return true if the given contextual type is a tuple-like type function contextualTypeIsTupleLikeType(type: Type): boolean { return !!(type.flags & TypeFlags.Union ? forEach((type).types, isTupleLikeType) : isTupleLikeType(type)); @@ -10350,22 +10358,14 @@ namespace ts { return getUnionType([type1, type2]); } - function checkStringLiteralExpression(node: LiteralExpression) { + function checkStringLiteralExpression(node: StringLiteral): Type { // TODO (drosen): Do we want to apply the same approach to no-sub template literals? let contextualType = getContextualType(node); - if (contextualType) { - if (contextualType.flags & TypeFlags.Union) { - for (const type of (contextualType).types) { - if (type.flags & TypeFlags.StringLiteral && (type).text === node.text) { - return type; - } - } - } - else if (contextualType.flags & TypeFlags.StringLiteral && (contextualType).text === node.text) { - return contextualType; - } + if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { + return getStringLiteralType(node); } + return stringType; } @@ -10499,7 +10499,7 @@ namespace ts { case SyntaxKind.TemplateExpression: return checkTemplateExpression(node); case SyntaxKind.StringLiteral: - return checkStringLiteralExpression(node); + return checkStringLiteralExpression(node); case SyntaxKind.NoSubstitutionTemplateLiteral: return stringType; case SyntaxKind.RegularExpressionLiteral: From 1dbd8d1dd8309456f76ce46e7645cb0d4a646cd5 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 15 Oct 2015 14:26:41 -0700 Subject: [PATCH 28/38] Accepted baselines. --- ...loadOnConstantsInvalidOverload1.errors.txt | 4 ++-- .../overloadingOnConstants2.errors.txt | 4 ++-- ...gumentsWithStringLiteralTypes01.errors.txt | 24 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/baselines/reference/overloadOnConstantsInvalidOverload1.errors.txt b/tests/baselines/reference/overloadOnConstantsInvalidOverload1.errors.txt index 1676a2fb426a0..4fea7cc122795 100644 --- a/tests/baselines/reference/overloadOnConstantsInvalidOverload1.errors.txt +++ b/tests/baselines/reference/overloadOnConstantsInvalidOverload1.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(6,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(7,10): error TS2381: A signature with an implementation cannot use a string literal type. -tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(11,5): error TS2345: Argument of type 'string' is not assignable to parameter of type '"SPAN"'. +tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(11,5): error TS2345: Argument of type '"HI"' is not assignable to parameter of type '"SPAN"'. ==== tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts (3 errors) ==== @@ -20,4 +20,4 @@ tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(11,5): error TS2345: foo("HI"); ~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"SPAN"'. \ No newline at end of file +!!! error TS2345: Argument of type '"HI"' is not assignable to parameter of type '"SPAN"'. \ No newline at end of file diff --git a/tests/baselines/reference/overloadingOnConstants2.errors.txt b/tests/baselines/reference/overloadingOnConstants2.errors.txt index c6d6352357681..b448deaadc75b 100644 --- a/tests/baselines/reference/overloadingOnConstants2.errors.txt +++ b/tests/baselines/reference/overloadingOnConstants2.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/overloadingOnConstants2.ts(8,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/compiler/overloadingOnConstants2.ts(9,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/compiler/overloadingOnConstants2.ts(15,13): error TS2345: Argument of type 'string' is not assignable to parameter of type '"bye"'. +tests/cases/compiler/overloadingOnConstants2.ts(15,13): error TS2345: Argument of type '"um"' is not assignable to parameter of type '"bye"'. tests/cases/compiler/overloadingOnConstants2.ts(19,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. @@ -25,7 +25,7 @@ tests/cases/compiler/overloadingOnConstants2.ts(19,10): error TS2382: Specialize var b: E = foo("bye", []); // E var c = foo("um", []); // error ~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"bye"'. +!!! error TS2345: Argument of type '"um"' is not assignable to parameter of type '"bye"'. //function bar(x: "hi", items: string[]): D; diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt index 69f8d6581a1ae..49f17e7891de1 100644 --- a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt @@ -14,18 +14,18 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 Type 'string' is not assignable to type '"World"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(48,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. Type 'string' is not assignable to type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,52): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,43): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,52): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,43): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(61,5): error TS2322: Type 'string' is not assignable to type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(63,5): error TS2322: Type 'string' is not assignable to type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(75,5): error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. Type '"World"' is not assignable to type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(77,5): error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. Type '"World"' is not assignable to type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,43): error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. -tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,52): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,43): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,43): error TS2345: Argument of type '"Hello"' is not assignable to parameter of type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,52): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(93,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. Type 'string' is not assignable to type '"World"'. tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(97,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. @@ -120,14 +120,14 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 export let a = fun1<"Hello">("Hello", "Hello"); export let b = fun1<"Hello">("Hello", "World"); ~~~~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. export let c = fun2<"Hello", "Hello">("Hello", "Hello"); export let d = fun2<"Hello", "Hello">("Hello", "World"); ~~~~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. export let e = fun3<"Hello">("Hello", "World"); ~~~~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. // Assignment from the returned value should cause an error. a = takeReturnString(a); @@ -168,13 +168,13 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0 export let a = fun2<"Hello", "World">("Hello", "World"); export let b = fun2<"Hello", "World">("World", "Hello"); ~~~~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. export let c = fun2<"World", "Hello">("Hello", "Hello"); ~~~~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"World"'. +!!! error TS2345: Argument of type '"Hello"' is not assignable to parameter of type '"World"'. export let d = fun2<"World", "Hello">("World", "World"); ~~~~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. export let e = fun3<"Hello" | "World">("Hello", "World"); // Assignment from the returned value should cause an error. From 6618fd21bfe5e34a9d0014d5a60f8ef97e0b91c8 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 26 Oct 2015 15:42:02 -0700 Subject: [PATCH 29/38] Added tests for operations that use assignable to/from. --- .../stringLiteralCheckedInIf01.ts | 17 ++++++++++ .../stringLiteralCheckedInIf02.ts | 18 +++++++++++ .../stringLiteralMatchedInSwitch01.ts | 13 ++++++++ .../stringLiteralTypeAssertion01.ts | 31 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts new file mode 100644 index 0000000000000..093824acf0d42 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts @@ -0,0 +1,17 @@ + +type S = "a" | "b"; +type T = S[] | S; + +function f(foo: T) { + if (foo === "a") { + return foo; + } + else if (foo === "b") { + return foo; + } + else { + return (foo as S[])[0]; + } + + throw new Error("Unreachable code hit."); +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts new file mode 100644 index 0000000000000..53d625f720034 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts @@ -0,0 +1,18 @@ + +type S = "a" | "b"; +type T = S[] | S; + +function isS(t: T): t is S { + return t === "a" || t === "b"; +} + +function f(foo: T) { + if (isS(foo)) { + return foo; + } + else { + return foo[0]; + } + + throw new Error("Unreachable code hit."); +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts new file mode 100644 index 0000000000000..141f5d12795ef --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts @@ -0,0 +1,13 @@ + +type S = "a" | "b"; +type T = S[] | S; + +var foo: T; +switch (foo) { + case "a": + case "b": + break; + default: + foo = (foo as S[])[0]; + break; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts new file mode 100644 index 0000000000000..ec1f1e5eb5634 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts @@ -0,0 +1,31 @@ + +type S = "a" | "b"; +type T = S[] | S; + +var s: S; +var t: T; +var str: string; + +//////////////// + +s = t; +s = t as S; + +s = str; +s = str as S; + +//////////////// + +t = s; +t = s as T; + +t = str; +t = str as T; + +//////////////// + +str = s; +str = s as string; + +str = t; +str = t as string; From 5e2314303f89ac4dae495700471c9f6d8f5f3303 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 26 Oct 2015 18:49:41 -0700 Subject: [PATCH 30/38] Accepted baselines. --- .../stringLiteralCheckedInIf01.errors.txt | 26 +++++++++ .../reference/stringLiteralCheckedInIf01.js | 32 +++++++++++ .../stringLiteralCheckedInIf02.errors.txt | 27 +++++++++ .../reference/stringLiteralCheckedInIf02.js | 33 +++++++++++ .../stringLiteralMatchedInSwitch01.errors.txt | 26 +++++++++ .../stringLiteralMatchedInSwitch01.js | 25 +++++++++ .../stringLiteralTypeAssertion01.errors.txt | 55 +++++++++++++++++++ .../reference/stringLiteralTypeAssertion01.js | 53 ++++++++++++++++++ 8 files changed, 277 insertions(+) create mode 100644 tests/baselines/reference/stringLiteralCheckedInIf01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralCheckedInIf01.js create mode 100644 tests/baselines/reference/stringLiteralCheckedInIf02.errors.txt create mode 100644 tests/baselines/reference/stringLiteralCheckedInIf02.js create mode 100644 tests/baselines/reference/stringLiteralMatchedInSwitch01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralMatchedInSwitch01.js create mode 100644 tests/baselines/reference/stringLiteralTypeAssertion01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypeAssertion01.js diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.errors.txt b/tests/baselines/reference/stringLiteralCheckedInIf01.errors.txt new file mode 100644 index 0000000000000..fb94cf6bc96d7 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.errors.txt @@ -0,0 +1,26 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts(6,9): error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. +tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts(9,14): error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts (2 errors) ==== + + type S = "a" | "b"; + type T = S[] | S; + + function f(foo: T) { + if (foo === "a") { + ~~~~~~~~~~~ +!!! error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. + return foo; + } + else if (foo === "b") { + ~~~~~~~~~~~ +!!! error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. + return foo; + } + else { + return (foo as S[])[0]; + } + + throw new Error("Unreachable code hit."); + } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.js b/tests/baselines/reference/stringLiteralCheckedInIf01.js new file mode 100644 index 0000000000000..227a243133c6b --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.js @@ -0,0 +1,32 @@ +//// [stringLiteralCheckedInIf01.ts] + +type S = "a" | "b"; +type T = S[] | S; + +function f(foo: T) { + if (foo === "a") { + return foo; + } + else if (foo === "b") { + return foo; + } + else { + return (foo as S[])[0]; + } + + throw new Error("Unreachable code hit."); +} + +//// [stringLiteralCheckedInIf01.js] +function f(foo) { + if (foo === "a") { + return foo; + } + else if (foo === "b") { + return foo; + } + else { + return foo[0]; + } + throw new Error("Unreachable code hit."); +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.errors.txt b/tests/baselines/reference/stringLiteralCheckedInIf02.errors.txt new file mode 100644 index 0000000000000..ced5adf626334 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.errors.txt @@ -0,0 +1,27 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts(6,12): error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. +tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts(6,25): error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts (2 errors) ==== + + type S = "a" | "b"; + type T = S[] | S; + + function isS(t: T): t is S { + return t === "a" || t === "b"; + ~~~~~~~~~ +!!! error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. + ~~~~~~~~~ +!!! error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. + } + + function f(foo: T) { + if (isS(foo)) { + return foo; + } + else { + return foo[0]; + } + + throw new Error("Unreachable code hit."); + } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.js b/tests/baselines/reference/stringLiteralCheckedInIf02.js new file mode 100644 index 0000000000000..b0b3a64116b3c --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.js @@ -0,0 +1,33 @@ +//// [stringLiteralCheckedInIf02.ts] + +type S = "a" | "b"; +type T = S[] | S; + +function isS(t: T): t is S { + return t === "a" || t === "b"; +} + +function f(foo: T) { + if (isS(foo)) { + return foo; + } + else { + return foo[0]; + } + + throw new Error("Unreachable code hit."); +} + +//// [stringLiteralCheckedInIf02.js] +function isS(t) { + return t === "a" || t === "b"; +} +function f(foo) { + if (isS(foo)) { + return foo; + } + else { + return foo[0]; + } + throw new Error("Unreachable code hit."); +} diff --git a/tests/baselines/reference/stringLiteralMatchedInSwitch01.errors.txt b/tests/baselines/reference/stringLiteralMatchedInSwitch01.errors.txt new file mode 100644 index 0000000000000..17e690715a1c5 --- /dev/null +++ b/tests/baselines/reference/stringLiteralMatchedInSwitch01.errors.txt @@ -0,0 +1,26 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts(7,10): error TS2322: Type 'string' is not assignable to type '("a" | "b")[] | "a" | "b"'. + Type 'string' is not assignable to type '"b"'. +tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts(8,10): error TS2322: Type 'string' is not assignable to type '("a" | "b")[] | "a" | "b"'. + Type 'string' is not assignable to type '"b"'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts (2 errors) ==== + + type S = "a" | "b"; + type T = S[] | S; + + var foo: T; + switch (foo) { + case "a": + ~~~ +!!! error TS2322: Type 'string' is not assignable to type '("a" | "b")[] | "a" | "b"'. +!!! error TS2322: Type 'string' is not assignable to type '"b"'. + case "b": + ~~~ +!!! error TS2322: Type 'string' is not assignable to type '("a" | "b")[] | "a" | "b"'. +!!! error TS2322: Type 'string' is not assignable to type '"b"'. + break; + default: + foo = (foo as S[])[0]; + break; + } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralMatchedInSwitch01.js b/tests/baselines/reference/stringLiteralMatchedInSwitch01.js new file mode 100644 index 0000000000000..b9b24cf0c66b3 --- /dev/null +++ b/tests/baselines/reference/stringLiteralMatchedInSwitch01.js @@ -0,0 +1,25 @@ +//// [stringLiteralMatchedInSwitch01.ts] + +type S = "a" | "b"; +type T = S[] | S; + +var foo: T; +switch (foo) { + case "a": + case "b": + break; + default: + foo = (foo as S[])[0]; + break; +} + +//// [stringLiteralMatchedInSwitch01.js] +var foo; +switch (foo) { + case "a": + case "b": + break; + default: + foo = foo[0]; + break; +} diff --git a/tests/baselines/reference/stringLiteralTypeAssertion01.errors.txt b/tests/baselines/reference/stringLiteralTypeAssertion01.errors.txt new file mode 100644 index 0000000000000..867ad80ee7fbf --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypeAssertion01.errors.txt @@ -0,0 +1,55 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts(22,5): error TS2352: Neither type 'string' nor type '("a" | "b")[] | "a" | "b"' is assignable to the other. + Type 'string' is not assignable to type '"b"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts(23,5): error TS2352: Neither type 'string' nor type '("a" | "b")[] | "a" | "b"' is assignable to the other. + Type 'string' is not assignable to type '"b"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts(30,7): error TS2352: Neither type '("a" | "b")[] | "a" | "b"' nor type 'string' is assignable to the other. + Type '("a" | "b")[]' is not assignable to type 'string'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts(31,7): error TS2352: Neither type '("a" | "b")[] | "a" | "b"' nor type 'string' is assignable to the other. + Type '("a" | "b")[]' is not assignable to type 'string'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts (4 errors) ==== + + type S = "a" | "b"; + type T = S[] | S; + + var s: S; + var t: T; + var str: string; + + //////////////// + + s = t; + s = t as S; + + s = str; + s = str as S; + + //////////////// + + t = s; + t = s as T; + + t = str; + ~~~~~~ +!!! error TS2352: Neither type 'string' nor type '("a" | "b")[] | "a" | "b"' is assignable to the other. +!!! error TS2352: Type 'string' is not assignable to type '"b"'. + t = str as T; + ~~~~~~~~ +!!! error TS2352: Neither type 'string' nor type '("a" | "b")[] | "a" | "b"' is assignable to the other. +!!! error TS2352: Type 'string' is not assignable to type '"b"'. + + //////////////// + + str = s; + str = s as string; + + str = t; + ~~~~~~~~~ +!!! error TS2352: Neither type '("a" | "b")[] | "a" | "b"' nor type 'string' is assignable to the other. +!!! error TS2352: Type '("a" | "b")[]' is not assignable to type 'string'. + str = t as string; + ~~~~~~~~~~~ +!!! error TS2352: Neither type '("a" | "b")[] | "a" | "b"' nor type 'string' is assignable to the other. +!!! error TS2352: Type '("a" | "b")[]' is not assignable to type 'string'. + \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypeAssertion01.js b/tests/baselines/reference/stringLiteralTypeAssertion01.js new file mode 100644 index 0000000000000..7877004cf01ab --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypeAssertion01.js @@ -0,0 +1,53 @@ +//// [stringLiteralTypeAssertion01.ts] + +type S = "a" | "b"; +type T = S[] | S; + +var s: S; +var t: T; +var str: string; + +//////////////// + +s = t; +s = t as S; + +s = str; +s = str as S; + +//////////////// + +t = s; +t = s as T; + +t = str; +t = str as T; + +//////////////// + +str = s; +str = s as string; + +str = t; +str = t as string; + + +//// [stringLiteralTypeAssertion01.js] +var s; +var t; +var str; +//////////////// +s = t; +s = t; +s = str; +s = str; +//////////////// +t = s; +t = s; +t = str; +t = str; +//////////////// +str = s; +str = s; +str = t; +str = t; From 8dbfe1ca631111e28a59b62e1ddc941ea6fe2e3f Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 6 Nov 2015 12:58:03 -0800 Subject: [PATCH 31/38] Added specific checks for comparing stringlike types. --- src/compiler/checker.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 50b826c1d14e0..3bbc27d71b0fc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9400,7 +9400,12 @@ namespace ts { let targetType = getTypeFromTypeNode(node.type); if (produceDiagnostics && targetType !== unknownType) { let widenedType = getWidenedType(exprType); - if (!(isTypeAssignableTo(targetType, widenedType))) { + + // Permit 'number[] | "foo"' to be asserted to 'string'. + const bothAreStringLike = + someConstituentTypeHasKind(targetType, TypeFlags.StringLike) && + someConstituentTypeHasKind(widenedType, TypeFlags.StringLike); + if (!bothAreStringLike && !(isTypeAssignableTo(targetType, widenedType))) { checkTypeAssignableTo(exprType, targetType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } @@ -10244,6 +10249,10 @@ namespace ts { case SyntaxKind.ExclamationEqualsToken: case SyntaxKind.EqualsEqualsEqualsToken: case SyntaxKind.ExclamationEqualsEqualsToken: + // Permit 'number[] | "foo"' to be asserted to 'string'. + if (someConstituentTypeHasKind(leftType, TypeFlags.StringLike) && someConstituentTypeHasKind(rightType, TypeFlags.StringLike)) { + return booleanType; + } if (!isTypeAssignableTo(leftType, rightType) && !isTypeAssignableTo(rightType, leftType)) { reportOperatorError(); } @@ -12703,6 +12712,7 @@ namespace ts { let hasDuplicateDefaultClause = false; let expressionType = checkExpression(node.expression); + const expressionTypeIsStringLike = someConstituentTypeHasKind(expressionType, TypeFlags.StringLike); forEach(node.caseBlock.clauses, clause => { // Grammar check for duplicate default clauses, skip if we already report duplicate default clause if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) { @@ -12723,6 +12733,12 @@ namespace ts { // TypeScript 1.0 spec (April 2014):5.9 // In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression. let caseType = checkExpression(caseClause.expression); + + // Permit 'number[] | "foo"' to be asserted to 'string'. + if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, TypeFlags.StringLike)) { + return; + } + if (!isTypeAssignableTo(expressionType, caseType)) { // check 'expressionType isAssignableTo caseType' failed, try the reversed check and report errors if it fails checkTypeAssignableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined); From a1fcfaf5744bdf33767cb9cc720e2cc636cfe213 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 6 Nov 2015 12:59:06 -0800 Subject: [PATCH 32/38] Accepted baselines. --- .../stringLiteralCheckedInIf01.errors.txt | 26 ----- .../stringLiteralCheckedInIf01.symbols | 36 ++++++ .../stringLiteralCheckedInIf01.types | 46 ++++++++ .../stringLiteralCheckedInIf02.errors.txt | 27 ----- .../stringLiteralCheckedInIf02.symbols | 42 +++++++ .../stringLiteralCheckedInIf02.types | 52 +++++++++ .../stringLiteralMatchedInSwitch01.errors.txt | 26 ----- .../stringLiteralMatchedInSwitch01.symbols | 28 +++++ .../stringLiteralMatchedInSwitch01.types | 37 ++++++ .../stringLiteralTypeAssertion01.errors.txt | 55 --------- .../stringLiteralTypeAssertion01.symbols | 83 ++++++++++++++ .../stringLiteralTypeAssertion01.types | 107 ++++++++++++++++++ ...tringLiteralTypesInUnionTypes03.errors.txt | 29 ----- .../stringLiteralTypesInUnionTypes03.symbols | 52 +++++++++ .../stringLiteralTypesInUnionTypes03.types | 61 ++++++++++ ...eralTypesWithVariousOperators02.errors.txt | 13 +-- 16 files changed, 546 insertions(+), 174 deletions(-) delete mode 100644 tests/baselines/reference/stringLiteralCheckedInIf01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralCheckedInIf01.symbols create mode 100644 tests/baselines/reference/stringLiteralCheckedInIf01.types delete mode 100644 tests/baselines/reference/stringLiteralCheckedInIf02.errors.txt create mode 100644 tests/baselines/reference/stringLiteralCheckedInIf02.symbols create mode 100644 tests/baselines/reference/stringLiteralCheckedInIf02.types delete mode 100644 tests/baselines/reference/stringLiteralMatchedInSwitch01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralMatchedInSwitch01.symbols create mode 100644 tests/baselines/reference/stringLiteralMatchedInSwitch01.types delete mode 100644 tests/baselines/reference/stringLiteralTypeAssertion01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypeAssertion01.symbols create mode 100644 tests/baselines/reference/stringLiteralTypeAssertion01.types delete mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes03.symbols create mode 100644 tests/baselines/reference/stringLiteralTypesInUnionTypes03.types diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.errors.txt b/tests/baselines/reference/stringLiteralCheckedInIf01.errors.txt deleted file mode 100644 index fb94cf6bc96d7..0000000000000 --- a/tests/baselines/reference/stringLiteralCheckedInIf01.errors.txt +++ /dev/null @@ -1,26 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts(6,9): error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. -tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts(9,14): error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts (2 errors) ==== - - type S = "a" | "b"; - type T = S[] | S; - - function f(foo: T) { - if (foo === "a") { - ~~~~~~~~~~~ -!!! error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. - return foo; - } - else if (foo === "b") { - ~~~~~~~~~~~ -!!! error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. - return foo; - } - else { - return (foo as S[])[0]; - } - - throw new Error("Unreachable code hit."); - } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.symbols b/tests/baselines/reference/stringLiteralCheckedInIf01.symbols new file mode 100644 index 0000000000000..bd382a66ffa96 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.symbols @@ -0,0 +1,36 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts === + +type S = "a" | "b"; +>S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) + +type T = S[] | S; +>T : Symbol(T, Decl(stringLiteralCheckedInIf01.ts, 1, 19)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) + +function f(foo: T) { +>f : Symbol(f, Decl(stringLiteralCheckedInIf01.ts, 2, 17)) +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) +>T : Symbol(T, Decl(stringLiteralCheckedInIf01.ts, 1, 19)) + + if (foo === "a") { +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) + + return foo; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) + } + else if (foo === "b") { +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) + + return foo; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) + } + else { + return (foo as S[])[0]; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) + } + + throw new Error("Unreachable code hit."); +>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.types b/tests/baselines/reference/stringLiteralCheckedInIf01.types new file mode 100644 index 0000000000000..1b3e663be740f --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.types @@ -0,0 +1,46 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts === + +type S = "a" | "b"; +>S : "a" | "b" + +type T = S[] | S; +>T : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>S : "a" | "b" + +function f(foo: T) { +>f : (foo: ("a" | "b")[] | "a" | "b") => ("a" | "b")[] | "a" | "b" +>foo : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + + if (foo === "a") { +>foo === "a" : boolean +>foo : ("a" | "b")[] | "a" | "b" +>"a" : string + + return foo; +>foo : ("a" | "b")[] | "a" | "b" + } + else if (foo === "b") { +>foo === "b" : boolean +>foo : ("a" | "b")[] | "a" | "b" +>"b" : string + + return foo; +>foo : ("a" | "b")[] | "a" | "b" + } + else { + return (foo as S[])[0]; +>(foo as S[])[0] : "a" | "b" +>(foo as S[]) : ("a" | "b")[] +>foo as S[] : ("a" | "b")[] +>foo : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>0 : number + } + + throw new Error("Unreachable code hit."); +>new Error("Unreachable code hit.") : Error +>Error : ErrorConstructor +>"Unreachable code hit." : string +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.errors.txt b/tests/baselines/reference/stringLiteralCheckedInIf02.errors.txt deleted file mode 100644 index ced5adf626334..0000000000000 --- a/tests/baselines/reference/stringLiteralCheckedInIf02.errors.txt +++ /dev/null @@ -1,27 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts(6,12): error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. -tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts(6,25): error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts (2 errors) ==== - - type S = "a" | "b"; - type T = S[] | S; - - function isS(t: T): t is S { - return t === "a" || t === "b"; - ~~~~~~~~~ -!!! error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. - ~~~~~~~~~ -!!! error TS2365: Operator '===' cannot be applied to types '("a" | "b")[] | "a" | "b"' and 'string'. - } - - function f(foo: T) { - if (isS(foo)) { - return foo; - } - else { - return foo[0]; - } - - throw new Error("Unreachable code hit."); - } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.symbols b/tests/baselines/reference/stringLiteralCheckedInIf02.symbols new file mode 100644 index 0000000000000..84a19936371b2 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.symbols @@ -0,0 +1,42 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts === + +type S = "a" | "b"; +>S : Symbol(S, Decl(stringLiteralCheckedInIf02.ts, 0, 0)) + +type T = S[] | S; +>T : Symbol(T, Decl(stringLiteralCheckedInIf02.ts, 1, 19)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf02.ts, 0, 0)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf02.ts, 0, 0)) + +function isS(t: T): t is S { +>isS : Symbol(isS, Decl(stringLiteralCheckedInIf02.ts, 2, 17)) +>t : Symbol(t, Decl(stringLiteralCheckedInIf02.ts, 4, 13)) +>T : Symbol(T, Decl(stringLiteralCheckedInIf02.ts, 1, 19)) +>t : Symbol(t, Decl(stringLiteralCheckedInIf02.ts, 4, 13)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf02.ts, 0, 0)) + + return t === "a" || t === "b"; +>t : Symbol(t, Decl(stringLiteralCheckedInIf02.ts, 4, 13)) +>t : Symbol(t, Decl(stringLiteralCheckedInIf02.ts, 4, 13)) +} + +function f(foo: T) { +>f : Symbol(f, Decl(stringLiteralCheckedInIf02.ts, 6, 1)) +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) +>T : Symbol(T, Decl(stringLiteralCheckedInIf02.ts, 1, 19)) + + if (isS(foo)) { +>isS : Symbol(isS, Decl(stringLiteralCheckedInIf02.ts, 2, 17)) +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) + + return foo; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) + } + else { + return foo[0]; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) + } + + throw new Error("Unreachable code hit."); +>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.types b/tests/baselines/reference/stringLiteralCheckedInIf02.types new file mode 100644 index 0000000000000..005ec356f39b9 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.types @@ -0,0 +1,52 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts === + +type S = "a" | "b"; +>S : "a" | "b" + +type T = S[] | S; +>T : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>S : "a" | "b" + +function isS(t: T): t is S { +>isS : (t: ("a" | "b")[] | "a" | "b") => t is "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" +>t : any +>S : "a" | "b" + + return t === "a" || t === "b"; +>t === "a" || t === "b" : boolean +>t === "a" : boolean +>t : ("a" | "b")[] | "a" | "b" +>"a" : string +>t === "b" : boolean +>t : ("a" | "b")[] | "a" | "b" +>"b" : string +} + +function f(foo: T) { +>f : (foo: ("a" | "b")[] | "a" | "b") => "a" | "b" +>foo : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + + if (isS(foo)) { +>isS(foo) : boolean +>isS : (t: ("a" | "b")[] | "a" | "b") => t is "a" | "b" +>foo : ("a" | "b")[] | "a" | "b" + + return foo; +>foo : "a" | "b" + } + else { + return foo[0]; +>foo[0] : "a" | "b" +>foo : ("a" | "b")[] +>0 : number + } + + throw new Error("Unreachable code hit."); +>new Error("Unreachable code hit.") : Error +>Error : ErrorConstructor +>"Unreachable code hit." : string +} diff --git a/tests/baselines/reference/stringLiteralMatchedInSwitch01.errors.txt b/tests/baselines/reference/stringLiteralMatchedInSwitch01.errors.txt deleted file mode 100644 index 17e690715a1c5..0000000000000 --- a/tests/baselines/reference/stringLiteralMatchedInSwitch01.errors.txt +++ /dev/null @@ -1,26 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts(7,10): error TS2322: Type 'string' is not assignable to type '("a" | "b")[] | "a" | "b"'. - Type 'string' is not assignable to type '"b"'. -tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts(8,10): error TS2322: Type 'string' is not assignable to type '("a" | "b")[] | "a" | "b"'. - Type 'string' is not assignable to type '"b"'. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts (2 errors) ==== - - type S = "a" | "b"; - type T = S[] | S; - - var foo: T; - switch (foo) { - case "a": - ~~~ -!!! error TS2322: Type 'string' is not assignable to type '("a" | "b")[] | "a" | "b"'. -!!! error TS2322: Type 'string' is not assignable to type '"b"'. - case "b": - ~~~ -!!! error TS2322: Type 'string' is not assignable to type '("a" | "b")[] | "a" | "b"'. -!!! error TS2322: Type 'string' is not assignable to type '"b"'. - break; - default: - foo = (foo as S[])[0]; - break; - } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralMatchedInSwitch01.symbols b/tests/baselines/reference/stringLiteralMatchedInSwitch01.symbols new file mode 100644 index 0000000000000..0323371c3070d --- /dev/null +++ b/tests/baselines/reference/stringLiteralMatchedInSwitch01.symbols @@ -0,0 +1,28 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts === + +type S = "a" | "b"; +>S : Symbol(S, Decl(stringLiteralMatchedInSwitch01.ts, 0, 0)) + +type T = S[] | S; +>T : Symbol(T, Decl(stringLiteralMatchedInSwitch01.ts, 1, 19)) +>S : Symbol(S, Decl(stringLiteralMatchedInSwitch01.ts, 0, 0)) +>S : Symbol(S, Decl(stringLiteralMatchedInSwitch01.ts, 0, 0)) + +var foo: T; +>foo : Symbol(foo, Decl(stringLiteralMatchedInSwitch01.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralMatchedInSwitch01.ts, 1, 19)) + +switch (foo) { +>foo : Symbol(foo, Decl(stringLiteralMatchedInSwitch01.ts, 4, 3)) + + case "a": + case "b": + break; + default: + foo = (foo as S[])[0]; +>foo : Symbol(foo, Decl(stringLiteralMatchedInSwitch01.ts, 4, 3)) +>foo : Symbol(foo, Decl(stringLiteralMatchedInSwitch01.ts, 4, 3)) +>S : Symbol(S, Decl(stringLiteralMatchedInSwitch01.ts, 0, 0)) + + break; +} diff --git a/tests/baselines/reference/stringLiteralMatchedInSwitch01.types b/tests/baselines/reference/stringLiteralMatchedInSwitch01.types new file mode 100644 index 0000000000000..cfbb77e07e0a3 --- /dev/null +++ b/tests/baselines/reference/stringLiteralMatchedInSwitch01.types @@ -0,0 +1,37 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts === + +type S = "a" | "b"; +>S : "a" | "b" + +type T = S[] | S; +>T : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>S : "a" | "b" + +var foo: T; +>foo : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + +switch (foo) { +>foo : ("a" | "b")[] | "a" | "b" + + case "a": +>"a" : string + + case "b": +>"b" : string + + break; + default: + foo = (foo as S[])[0]; +>foo = (foo as S[])[0] : "a" | "b" +>foo : ("a" | "b")[] | "a" | "b" +>(foo as S[])[0] : "a" | "b" +>(foo as S[]) : ("a" | "b")[] +>foo as S[] : ("a" | "b")[] +>foo : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>0 : number + + break; +} diff --git a/tests/baselines/reference/stringLiteralTypeAssertion01.errors.txt b/tests/baselines/reference/stringLiteralTypeAssertion01.errors.txt deleted file mode 100644 index 867ad80ee7fbf..0000000000000 --- a/tests/baselines/reference/stringLiteralTypeAssertion01.errors.txt +++ /dev/null @@ -1,55 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts(22,5): error TS2352: Neither type 'string' nor type '("a" | "b")[] | "a" | "b"' is assignable to the other. - Type 'string' is not assignable to type '"b"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts(23,5): error TS2352: Neither type 'string' nor type '("a" | "b")[] | "a" | "b"' is assignable to the other. - Type 'string' is not assignable to type '"b"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts(30,7): error TS2352: Neither type '("a" | "b")[] | "a" | "b"' nor type 'string' is assignable to the other. - Type '("a" | "b")[]' is not assignable to type 'string'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts(31,7): error TS2352: Neither type '("a" | "b")[] | "a" | "b"' nor type 'string' is assignable to the other. - Type '("a" | "b")[]' is not assignable to type 'string'. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts (4 errors) ==== - - type S = "a" | "b"; - type T = S[] | S; - - var s: S; - var t: T; - var str: string; - - //////////////// - - s = t; - s = t as S; - - s = str; - s = str as S; - - //////////////// - - t = s; - t = s as T; - - t = str; - ~~~~~~ -!!! error TS2352: Neither type 'string' nor type '("a" | "b")[] | "a" | "b"' is assignable to the other. -!!! error TS2352: Type 'string' is not assignable to type '"b"'. - t = str as T; - ~~~~~~~~ -!!! error TS2352: Neither type 'string' nor type '("a" | "b")[] | "a" | "b"' is assignable to the other. -!!! error TS2352: Type 'string' is not assignable to type '"b"'. - - //////////////// - - str = s; - str = s as string; - - str = t; - ~~~~~~~~~ -!!! error TS2352: Neither type '("a" | "b")[] | "a" | "b"' nor type 'string' is assignable to the other. -!!! error TS2352: Type '("a" | "b")[]' is not assignable to type 'string'. - str = t as string; - ~~~~~~~~~~~ -!!! error TS2352: Neither type '("a" | "b")[] | "a" | "b"' nor type 'string' is assignable to the other. -!!! error TS2352: Type '("a" | "b")[]' is not assignable to type 'string'. - \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypeAssertion01.symbols b/tests/baselines/reference/stringLiteralTypeAssertion01.symbols new file mode 100644 index 0000000000000..1491a32b6f8bf --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypeAssertion01.symbols @@ -0,0 +1,83 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts === + +type S = "a" | "b"; +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +type T = S[] | S; +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +var s: S; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +var t: T; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) + +var str: string; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) + +//////////////// + +s = t; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) + +s = t as S; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +s = str; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) + +s = str as S; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +//////////////// + +t = s; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) + +t = s as T; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) + +t = str; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) + +t = str as T; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) + +//////////////// + +str = s; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) + +str = s as string; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) + +str = t; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) + +str = t as string; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypeAssertion01.types b/tests/baselines/reference/stringLiteralTypeAssertion01.types new file mode 100644 index 0000000000000..f70313f834700 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypeAssertion01.types @@ -0,0 +1,107 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts === + +type S = "a" | "b"; +>S : "a" | "b" + +type T = S[] | S; +>T : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>S : "a" | "b" + +var s: S; +>s : "a" | "b" +>S : "a" | "b" + +var t: T; +>t : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + +var str: string; +>str : string + +//////////////// + +s = t; +>s = t : "a" | "b" +>s : "a" | "b" +>t : "a" | "b" +>S : "a" | "b" +>t : ("a" | "b")[] | "a" | "b" + +s = t as S; +>s = t as S : "a" | "b" +>s : "a" | "b" +>t as S : "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" + +s = str; +>s = str : "a" | "b" +>s : "a" | "b" +>str : "a" | "b" +>S : "a" | "b" +>str : string + +s = str as S; +>s = str as S : "a" | "b" +>s : "a" | "b" +>str as S : "a" | "b" +>str : string +>S : "a" | "b" + +//////////////// + +t = s; +>t = s : ("a" | "b")[] | "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>s : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" +>s : "a" | "b" + +t = s as T; +>t = s as T : ("a" | "b")[] | "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>s as T : ("a" | "b")[] | "a" | "b" +>s : "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + +t = str; +>t = str : ("a" | "b")[] | "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>str : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" +>str : string + +t = str as T; +>t = str as T : ("a" | "b")[] | "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>str as T : ("a" | "b")[] | "a" | "b" +>str : string +>T : ("a" | "b")[] | "a" | "b" + +//////////////// + +str = s; +>str = s : string +>str : string +>s : string +>s : "a" | "b" + +str = s as string; +>str = s as string : string +>str : string +>s as string : string +>s : "a" | "b" + +str = t; +>str = t : string +>str : string +>t : string +>t : ("a" | "b")[] | "a" | "b" + +str = t as string; +>str = t as string : string +>str : string +>t as string : string +>t : ("a" | "b")[] | "a" | "b" + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt deleted file mode 100644 index 74249b178d619..0000000000000 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.errors.txt +++ /dev/null @@ -1,29 +0,0 @@ -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(7,5): error TS2365: Operator '===' cannot be applied to types '"foo" | "bar" | number' and 'string'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts(10,10): error TS2365: Operator '!==' cannot be applied to types '"foo" | "bar" | number' and 'string'. - - -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts (2 errors) ==== - - type T = number | "foo" | "bar"; - - var x: "foo" | "bar" | number; - var y: T = "bar"; - - if (x === "foo") { - ~~~~~~~~~~~ -!!! error TS2365: Operator '===' cannot be applied to types '"foo" | "bar" | number' and 'string'. - let a = x; - } - else if (x !== "bar") { - ~~~~~~~~~~~ -!!! error TS2365: Operator '!==' cannot be applied to types '"foo" | "bar" | number' and 'string'. - let b = x || y; - } - else { - let c = x; - let d = y; - let e: (typeof x) | (typeof y) = c || d; - } - - x = y; - y = x; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.symbols b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.symbols new file mode 100644 index 0000000000000..df5498d9a5943 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.symbols @@ -0,0 +1,52 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts === + +type T = number | "foo" | "bar"; +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes03.ts, 0, 0)) + +var x: "foo" | "bar" | number; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + +var y: T = "bar"; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes03.ts, 0, 0)) + +if (x === "foo") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + + let a = x; +>a : Symbol(a, Decl(stringLiteralTypesInUnionTypes03.ts, 7, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) +} +else if (x !== "bar") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + + let b = x || y; +>b : Symbol(b, Decl(stringLiteralTypesInUnionTypes03.ts, 10, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) +} +else { + let c = x; +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes03.ts, 13, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + + let d = y; +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes03.ts, 14, 7)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) + + let e: (typeof x) | (typeof y) = c || d; +>e : Symbol(e, Decl(stringLiteralTypesInUnionTypes03.ts, 15, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes03.ts, 13, 7)) +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes03.ts, 14, 7)) +} + +x = y; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) + +y = x; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.types new file mode 100644 index 0000000000000..5fca6e69be996 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.types @@ -0,0 +1,61 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts === + +type T = number | "foo" | "bar"; +>T : number | "foo" | "bar" + +var x: "foo" | "bar" | number; +>x : "foo" | "bar" | number + +var y: T = "bar"; +>y : number | "foo" | "bar" +>T : number | "foo" | "bar" +>"bar" : "bar" + +if (x === "foo") { +>x === "foo" : boolean +>x : "foo" | "bar" | number +>"foo" : string + + let a = x; +>a : "foo" | "bar" | number +>x : "foo" | "bar" | number +} +else if (x !== "bar") { +>x !== "bar" : boolean +>x : "foo" | "bar" | number +>"bar" : string + + let b = x || y; +>b : "foo" | "bar" | number +>x || y : "foo" | "bar" | number +>x : "foo" | "bar" | number +>y : number | "foo" | "bar" +} +else { + let c = x; +>c : "foo" | "bar" | number +>x : "foo" | "bar" | number + + let d = y; +>d : number | "foo" | "bar" +>y : number | "foo" | "bar" + + let e: (typeof x) | (typeof y) = c || d; +>e : "foo" | "bar" | number +>x : "foo" | "bar" | number +>y : number | "foo" | "bar" +>c || d : "foo" | "bar" | number +>c : "foo" | "bar" | number +>d : number | "foo" | "bar" +} + +x = y; +>x = y : number | "foo" | "bar" +>x : "foo" | "bar" | number +>y : number | "foo" | "bar" + +y = x; +>y = x : "foo" | "bar" | number +>y : number | "foo" | "bar" +>x : "foo" | "bar" | number + diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt index 92afe67df0c62..69dd6c2f6a955 100644 --- a/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt @@ -7,12 +7,9 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperato tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(13,11): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(14,9): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(15,9): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(16,9): error TS2365: Operator '<' cannot be applied to types '"ABC"' and '"XYZ"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(17,9): error TS2365: Operator '===' cannot be applied to types '"ABC"' and '"XYZ"'. -tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(18,9): error TS2365: Operator '!=' cannot be applied to types '"ABC"' and '"XYZ"'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts (12 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts (9 errors) ==== let abc: "ABC" = "ABC"; let xyz: "XYZ" = "XYZ"; @@ -47,11 +44,5 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperato ~~~~~~~~~~~~~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. let j = abc < xyz; - ~~~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '"ABC"' and '"XYZ"'. let k = abc === xyz; - ~~~~~~~~~~~ -!!! error TS2365: Operator '===' cannot be applied to types '"ABC"' and '"XYZ"'. - let l = abc != xyz; - ~~~~~~~~~~ -!!! error TS2365: Operator '!=' cannot be applied to types '"ABC"' and '"XYZ"'. \ No newline at end of file + let l = abc != xyz; \ No newline at end of file From f939ff23ce66ce8c461a2d5da400eb3c09fd384c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 6 Nov 2015 14:25:25 -0800 Subject: [PATCH 33/38] Fixed unreachable code in tests. --- .../types/stringLiteral/stringLiteralCheckedInIf01.ts | 2 -- .../types/stringLiteral/stringLiteralCheckedInIf02.ts | 2 -- 2 files changed, 4 deletions(-) diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts index 093824acf0d42..f13ce35e82914 100644 --- a/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts @@ -12,6 +12,4 @@ function f(foo: T) { else { return (foo as S[])[0]; } - - throw new Error("Unreachable code hit."); } \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts index 53d625f720034..4eaf18ca7ccb4 100644 --- a/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts @@ -13,6 +13,4 @@ function f(foo: T) { else { return foo[0]; } - - throw new Error("Unreachable code hit."); } \ No newline at end of file From d234b8df2ee3b26843d29e31e512c567ebf09e22 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 6 Nov 2015 14:28:54 -0800 Subject: [PATCH 34/38] Accepted baselines. --- tests/baselines/reference/stringLiteralCheckedInIf01.js | 3 --- tests/baselines/reference/stringLiteralCheckedInIf01.symbols | 3 --- tests/baselines/reference/stringLiteralCheckedInIf01.types | 5 ----- tests/baselines/reference/stringLiteralCheckedInIf02.js | 3 --- tests/baselines/reference/stringLiteralCheckedInIf02.symbols | 3 --- tests/baselines/reference/stringLiteralCheckedInIf02.types | 5 ----- 6 files changed, 22 deletions(-) diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.js b/tests/baselines/reference/stringLiteralCheckedInIf01.js index 227a243133c6b..161c39b96efa4 100644 --- a/tests/baselines/reference/stringLiteralCheckedInIf01.js +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.js @@ -13,8 +13,6 @@ function f(foo: T) { else { return (foo as S[])[0]; } - - throw new Error("Unreachable code hit."); } //// [stringLiteralCheckedInIf01.js] @@ -28,5 +26,4 @@ function f(foo) { else { return foo[0]; } - throw new Error("Unreachable code hit."); } diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.symbols b/tests/baselines/reference/stringLiteralCheckedInIf01.symbols index bd382a66ffa96..1b4d5e6b07125 100644 --- a/tests/baselines/reference/stringLiteralCheckedInIf01.symbols +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.symbols @@ -30,7 +30,4 @@ function f(foo: T) { >foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) >S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) } - - throw new Error("Unreachable code hit."); ->Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) } diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.types b/tests/baselines/reference/stringLiteralCheckedInIf01.types index 1b3e663be740f..75b7eadbc19b4 100644 --- a/tests/baselines/reference/stringLiteralCheckedInIf01.types +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.types @@ -38,9 +38,4 @@ function f(foo: T) { >S : "a" | "b" >0 : number } - - throw new Error("Unreachable code hit."); ->new Error("Unreachable code hit.") : Error ->Error : ErrorConstructor ->"Unreachable code hit." : string } diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.js b/tests/baselines/reference/stringLiteralCheckedInIf02.js index b0b3a64116b3c..22bd6359f6187 100644 --- a/tests/baselines/reference/stringLiteralCheckedInIf02.js +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.js @@ -14,8 +14,6 @@ function f(foo: T) { else { return foo[0]; } - - throw new Error("Unreachable code hit."); } //// [stringLiteralCheckedInIf02.js] @@ -29,5 +27,4 @@ function f(foo) { else { return foo[0]; } - throw new Error("Unreachable code hit."); } diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.symbols b/tests/baselines/reference/stringLiteralCheckedInIf02.symbols index 84a19936371b2..629f990b91260 100644 --- a/tests/baselines/reference/stringLiteralCheckedInIf02.symbols +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.symbols @@ -36,7 +36,4 @@ function f(foo: T) { return foo[0]; >foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) } - - throw new Error("Unreachable code hit."); ->Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) } diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.types b/tests/baselines/reference/stringLiteralCheckedInIf02.types index 005ec356f39b9..79f4c6a223a71 100644 --- a/tests/baselines/reference/stringLiteralCheckedInIf02.types +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.types @@ -44,9 +44,4 @@ function f(foo: T) { >foo : ("a" | "b")[] >0 : number } - - throw new Error("Unreachable code hit."); ->new Error("Unreachable code hit.") : Error ->Error : ErrorConstructor ->"Unreachable code hit." : string } From c011ed455bd06551c170ee781189f1c02c4a7c1a Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 6 Nov 2015 15:00:35 -0800 Subject: [PATCH 35/38] Const. --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 44dcc423e8ff6..e0e9a6e620f54 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10403,7 +10403,7 @@ namespace ts { function checkStringLiteralExpression(node: StringLiteral): Type { // TODO (drosen): Do we want to apply the same approach to no-sub template literals? - let contextualType = getContextualType(node); + const contextualType = getContextualType(node); if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { return getStringLiteralType(node); } From 38090c6ba50f11fbfe4700b3771f95f194de4783 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 9 Nov 2015 12:48:58 -0800 Subject: [PATCH 36/38] Added tests for template strings with string literal types. --- .../stringLiteralTypesWithTemplateStrings01.ts | 7 +++++++ .../stringLiteralTypesWithTemplateStrings02.ts | 5 +++++ 2 files changed, 12 insertions(+) create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts create mode 100644 tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts new file mode 100644 index 0000000000000..1920551abd812 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts @@ -0,0 +1,7 @@ +// @declaration: true + +let ABC: "ABC" = `ABC`; +let DE_NEWLINE_F: "DE\nF" = `DE +F`; +let G_QUOTE_HI: 'G"HI'; +let JK_BACKTICK_L: "JK`L" = `JK\`L`; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts new file mode 100644 index 0000000000000..12debae4e2030 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts @@ -0,0 +1,5 @@ +// @declaration: true + +let abc: "AB\r\nC" = `AB +C`; +let de_NEWLINE_f: "DE\nF" = `DE${"\n"}F`; \ No newline at end of file From d294524861e9486c6f5d9c91a0557269206598e3 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 9 Nov 2015 13:27:08 -0800 Subject: [PATCH 37/38] Accepted baselines. --- ...teralTypesWithTemplateStrings01.errors.txt | 18 +++++++++++++++++ ...stringLiteralTypesWithTemplateStrings01.js | 20 +++++++++++++++++++ ...teralTypesWithTemplateStrings02.errors.txt | 13 ++++++++++++ ...stringLiteralTypesWithTemplateStrings02.js | 14 +++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.js create mode 100644 tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.errors.txt create mode 100644 tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.js diff --git a/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.errors.txt b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.errors.txt new file mode 100644 index 0000000000000..3851a0b10efaa --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.errors.txt @@ -0,0 +1,18 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts(2,5): error TS2322: Type 'string' is not assignable to type '"ABC"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts(3,5): error TS2322: Type 'string' is not assignable to type '"DE\nF"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts(6,5): error TS2322: Type 'string' is not assignable to type '"JK`L"'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts (3 errors) ==== + + let ABC: "ABC" = `ABC`; + ~~~ +!!! error TS2322: Type 'string' is not assignable to type '"ABC"'. + let DE_NEWLINE_F: "DE\nF" = `DE + ~~~~~~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"DE\nF"'. + F`; + let G_QUOTE_HI: 'G"HI'; + let JK_BACKTICK_L: "JK`L" = `JK\`L`; + ~~~~~~~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"JK`L"'. \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.js b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.js new file mode 100644 index 0000000000000..8f548afc7bf19 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.js @@ -0,0 +1,20 @@ +//// [stringLiteralTypesWithTemplateStrings01.ts] + +let ABC: "ABC" = `ABC`; +let DE_NEWLINE_F: "DE\nF" = `DE +F`; +let G_QUOTE_HI: 'G"HI'; +let JK_BACKTICK_L: "JK`L" = `JK\`L`; + +//// [stringLiteralTypesWithTemplateStrings01.js] +var ABC = "ABC"; +var DE_NEWLINE_F = "DE\nF"; +var G_QUOTE_HI; +var JK_BACKTICK_L = "JK`L"; + + +//// [stringLiteralTypesWithTemplateStrings01.d.ts] +declare let ABC: "ABC"; +declare let DE_NEWLINE_F: "DE\nF"; +declare let G_QUOTE_HI: 'G"HI'; +declare let JK_BACKTICK_L: "JK`L"; diff --git a/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.errors.txt b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.errors.txt new file mode 100644 index 0000000000000..803e4f5cb24e7 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.errors.txt @@ -0,0 +1,13 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts(2,5): error TS2322: Type 'string' is not assignable to type '"AB\r\nC"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts(4,5): error TS2322: Type 'string' is not assignable to type '"DE\nF"'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts (2 errors) ==== + + let abc: "AB\r\nC" = `AB + ~~~ +!!! error TS2322: Type 'string' is not assignable to type '"AB\r\nC"'. + C`; + let de_NEWLINE_f: "DE\nF" = `DE${"\n"}F`; + ~~~~~~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"DE\nF"'. \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.js b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.js new file mode 100644 index 0000000000000..f5d9045921a8e --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.js @@ -0,0 +1,14 @@ +//// [stringLiteralTypesWithTemplateStrings02.ts] + +let abc: "AB\r\nC" = `AB +C`; +let de_NEWLINE_f: "DE\nF" = `DE${"\n"}F`; + +//// [stringLiteralTypesWithTemplateStrings02.js] +var abc = "AB\nC"; +var de_NEWLINE_f = "DE" + "\n" + "F"; + + +//// [stringLiteralTypesWithTemplateStrings02.d.ts] +declare let abc: "AB\r\nC"; +declare let de_NEWLINE_f: "DE\nF"; From ea4e21d96914783c66563850eda52e0aafb7fb9b Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 9 Nov 2015 13:27:19 -0800 Subject: [PATCH 38/38] Fixed comments. --- src/compiler/checker.ts | 2 -- src/compiler/types.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e0e9a6e620f54..c3792c85dd647 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10401,8 +10401,6 @@ namespace ts { } function checkStringLiteralExpression(node: StringLiteral): Type { - // TODO (drosen): Do we want to apply the same approach to no-sub template literals? - const contextualType = getContextualType(node); if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { return getStringLiteralType(node); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f536a1e469307..d3cf86fed12c7 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -701,7 +701,7 @@ namespace ts { } // Note that a StringLiteral AST node is both an Expression and a TypeNode. The latter is - // because string literals can appear in the type annotation of a parameter node. + // because string literals can appear in type annotations as well. export interface StringLiteral extends LiteralExpression, TypeNode { _stringLiteralBrand: any; }