From 8755d4da47aa6ae3b054e27b8f4fa1ac83fb8bf9 Mon Sep 17 00:00:00 2001 From: kujon Date: Fri, 15 Dec 2017 22:28:29 +0000 Subject: [PATCH] Fixed an incorrect assumption K in Union works with numeric literals --- src/compiler/checker.ts | 2 +- .../reference/indexerConstraints2.errors.txt | 15 +++----------- .../reference/indexerConstraints2.js | 10 ++-------- .../reference/indexerConstraints2.symbols | 20 ++++--------------- .../reference/indexerConstraints2.types | 16 ++------------- tests/cases/compiler/indexerConstraints2.ts | 10 ++-------- 6 files changed, 14 insertions(+), 59 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 611b1bac6be..6c27a67c1d1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25781,7 +25781,7 @@ namespace ts { return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_cannot_be_a_type_alias_Use_index_Colon_0_instead, typeToString(type)); } - if (type.flags & TypeFlags.Union && every((type).types, t => !!(t.flags & TypeFlags.StringOrNumberLiteral))) { + if (type.flags & TypeFlags.Union && every((type).types, t => !!(t.flags & TypeFlags.StringLiteral))) { return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_cannot_be_a_union_type_Use_K_in_0_instead, symbolName(type.aliasSymbol)); } diff --git a/tests/baselines/reference/indexerConstraints2.errors.txt b/tests/baselines/reference/indexerConstraints2.errors.txt index f6ffcb25c7b..804ca4eafb5 100644 --- a/tests/baselines/reference/indexerConstraints2.errors.txt +++ b/tests/baselines/reference/indexerConstraints2.errors.txt @@ -8,10 +8,9 @@ tests/cases/compiler/indexerConstraints2.ts(52,6): error TS1337: An index signat tests/cases/compiler/indexerConstraints2.ts(58,6): error TS1023: An index signature parameter type must be 'string' or 'number'. tests/cases/compiler/indexerConstraints2.ts(64,6): error TS1023: An index signature parameter type must be 'string' or 'number'. tests/cases/compiler/indexerConstraints2.ts(70,6): error TS1023: An index signature parameter type must be 'string' or 'number'. -tests/cases/compiler/indexerConstraints2.ts(76,6): error TS1023: An index signature parameter type must be 'string' or 'number'. -==== tests/cases/compiler/indexerConstraints2.ts (11 errors) ==== +==== tests/cases/compiler/indexerConstraints2.ts (10 errors) ==== class A { a: number; } class B extends A { b: number; } @@ -72,7 +71,7 @@ tests/cases/compiler/indexerConstraints2.ts(76,6): error TS1023: An index signat !!! error TS1023: An index signature parameter type must be 'string' or 'number'. } - type IndexableUnion = "foo" | 42; + type IndexableUnion = "foo" | "bar"; interface O { [u: IndexableUnion]: A; @@ -96,18 +95,10 @@ tests/cases/compiler/indexerConstraints2.ts(76,6): error TS1023: An index signat !!! error TS1023: An index signature parameter type must be 'string' or 'number'. } - type NonIndexableUnion3 = 42 | string; + type NonIndexableUnion3 = "foo" | 42; interface R { [u: NonIndexableUnion3]: A; ~ -!!! error TS1023: An index signature parameter type must be 'string' or 'number'. - } - - type NonIndexableUnion4 = "foo" | number; - - interface S { - [u: NonIndexableUnion4]: A; - ~ !!! error TS1023: An index signature parameter type must be 'string' or 'number'. } \ No newline at end of file diff --git a/tests/baselines/reference/indexerConstraints2.js b/tests/baselines/reference/indexerConstraints2.js index 52df32907d5..7fae4839e3f 100644 --- a/tests/baselines/reference/indexerConstraints2.js +++ b/tests/baselines/reference/indexerConstraints2.js @@ -47,7 +47,7 @@ interface N { [b: AliasedBoolean]: A; } -type IndexableUnion = "foo" | 42; +type IndexableUnion = "foo" | "bar"; interface O { [u: IndexableUnion]: A; @@ -65,16 +65,10 @@ interface Q { [u: NonIndexableUnion2]: A; } -type NonIndexableUnion3 = 42 | string; +type NonIndexableUnion3 = "foo" | 42; interface R { [u: NonIndexableUnion3]: A; -} - -type NonIndexableUnion4 = "foo" | number; - -interface S { - [u: NonIndexableUnion4]: A; } //// [indexerConstraints2.js] diff --git a/tests/baselines/reference/indexerConstraints2.symbols b/tests/baselines/reference/indexerConstraints2.symbols index 571f29fafb9..fe171479ec1 100644 --- a/tests/baselines/reference/indexerConstraints2.symbols +++ b/tests/baselines/reference/indexerConstraints2.symbols @@ -100,11 +100,11 @@ interface N { >A : Symbol(A, Decl(indexerConstraints2.ts, 0, 0)) } -type IndexableUnion = "foo" | 42; +type IndexableUnion = "foo" | "bar"; >IndexableUnion : Symbol(IndexableUnion, Decl(indexerConstraints2.ts, 46, 1)) interface O { ->O : Symbol(O, Decl(indexerConstraints2.ts, 48, 33)) +>O : Symbol(O, Decl(indexerConstraints2.ts, 48, 36)) [u: IndexableUnion]: A; >u : Symbol(u, Decl(indexerConstraints2.ts, 51, 5)) @@ -136,26 +136,14 @@ interface Q { >A : Symbol(A, Decl(indexerConstraints2.ts, 0, 0)) } -type NonIndexableUnion3 = 42 | string; +type NonIndexableUnion3 = "foo" | 42; >NonIndexableUnion3 : Symbol(NonIndexableUnion3, Decl(indexerConstraints2.ts, 64, 1)) interface R { ->R : Symbol(R, Decl(indexerConstraints2.ts, 66, 38)) +>R : Symbol(R, Decl(indexerConstraints2.ts, 66, 37)) [u: NonIndexableUnion3]: A; >u : Symbol(u, Decl(indexerConstraints2.ts, 69, 5)) >NonIndexableUnion3 : Symbol(NonIndexableUnion3, Decl(indexerConstraints2.ts, 64, 1)) >A : Symbol(A, Decl(indexerConstraints2.ts, 0, 0)) } - -type NonIndexableUnion4 = "foo" | number; ->NonIndexableUnion4 : Symbol(NonIndexableUnion4, Decl(indexerConstraints2.ts, 70, 1)) - -interface S { ->S : Symbol(S, Decl(indexerConstraints2.ts, 72, 41)) - - [u: NonIndexableUnion4]: A; ->u : Symbol(u, Decl(indexerConstraints2.ts, 75, 5)) ->NonIndexableUnion4 : Symbol(NonIndexableUnion4, Decl(indexerConstraints2.ts, 70, 1)) ->A : Symbol(A, Decl(indexerConstraints2.ts, 0, 0)) -} diff --git a/tests/baselines/reference/indexerConstraints2.types b/tests/baselines/reference/indexerConstraints2.types index a7c830fe7ba..a41dda6ed35 100644 --- a/tests/baselines/reference/indexerConstraints2.types +++ b/tests/baselines/reference/indexerConstraints2.types @@ -100,7 +100,7 @@ interface N { >A : A } -type IndexableUnion = "foo" | 42; +type IndexableUnion = "foo" | "bar"; >IndexableUnion : IndexableUnion interface O { @@ -136,7 +136,7 @@ interface Q { >A : A } -type NonIndexableUnion3 = 42 | string; +type NonIndexableUnion3 = "foo" | 42; >NonIndexableUnion3 : NonIndexableUnion3 interface R { @@ -147,15 +147,3 @@ interface R { >NonIndexableUnion3 : NonIndexableUnion3 >A : A } - -type NonIndexableUnion4 = "foo" | number; ->NonIndexableUnion4 : NonIndexableUnion4 - -interface S { ->S : S - - [u: NonIndexableUnion4]: A; ->u : NonIndexableUnion4 ->NonIndexableUnion4 : NonIndexableUnion4 ->A : A -} diff --git a/tests/cases/compiler/indexerConstraints2.ts b/tests/cases/compiler/indexerConstraints2.ts index ac259df29d6..26cf94fbbc0 100644 --- a/tests/cases/compiler/indexerConstraints2.ts +++ b/tests/cases/compiler/indexerConstraints2.ts @@ -46,7 +46,7 @@ interface N { [b: AliasedBoolean]: A; } -type IndexableUnion = "foo" | 42; +type IndexableUnion = "foo" | "bar"; interface O { [u: IndexableUnion]: A; @@ -64,14 +64,8 @@ interface Q { [u: NonIndexableUnion2]: A; } -type NonIndexableUnion3 = 42 | string; +type NonIndexableUnion3 = "foo" | 42; interface R { [u: NonIndexableUnion3]: A; -} - -type NonIndexableUnion4 = "foo" | number; - -interface S { - [u: NonIndexableUnion4]: A; } \ No newline at end of file