Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -1970,15 +1970,11 @@ func (c *Checker) getSwitchClauseTypeOfWitnesses(node *ast.Node) []string {
witnesses := make([]string, len(clauses))
for i, clause := range clauses {
if clause.Kind == ast.KindCaseClause {
var text string
if ast.IsStringLiteralLike(clause.Expression()) {
text = clause.Expression().Text()
}
if text == "" {
if !ast.IsStringLiteralLike(clause.Expression()) {
witnesses = nil
break
}
if !slices.Contains(witnesses, text) {
if text := clause.Expression().Text(); !slices.Contains(witnesses, text) {
witnesses[i] = text
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
typeofSwitchEmptyStringCase.ts(5,10): error TS2678: Type '""' is not comparable to type '"bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"'.


==== typeofSwitchEmptyStringCase.ts (1 errors) ====
// https://github.com/microsoft/typescript-go/issues/3909

function f(x: string | number) {
switch (typeof x) {
case "":
~~
!!! error TS2678: Type '""' is not comparable to type '"bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"'.
case "string":
x.charAt(0);
break;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/typeofSwitchEmptyStringCase.ts] ////

=== typeofSwitchEmptyStringCase.ts ===
// https://github.com/microsoft/typescript-go/issues/3909

function f(x: string | number) {
>f : Symbol(f, Decl(typeofSwitchEmptyStringCase.ts, 0, 0))
>x : Symbol(x, Decl(typeofSwitchEmptyStringCase.ts, 2, 11))

switch (typeof x) {
>x : Symbol(x, Decl(typeofSwitchEmptyStringCase.ts, 2, 11))

case "":
case "string":
x.charAt(0);
>x.charAt : Symbol(String.charAt, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(typeofSwitchEmptyStringCase.ts, 2, 11))
>charAt : Symbol(String.charAt, Decl(lib.es5.d.ts, --, --))

break;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/typeofSwitchEmptyStringCase.ts] ////

=== typeofSwitchEmptyStringCase.ts ===
// https://github.com/microsoft/typescript-go/issues/3909

function f(x: string | number) {
>f : (x: string | number) => void
>x : string | number

switch (typeof x) {
>typeof x : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
>x : string | number

case "":
>"" : ""

case "string":
>"string" : "string"

x.charAt(0);
>x.charAt(0) : string
>x.charAt : (pos: number) => string
>x : string
>charAt : (pos: number) => string
>0 : 0

break;
}
}

12 changes: 12 additions & 0 deletions testdata/tests/cases/compiler/typeofSwitchEmptyStringCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @noEmit: true

// https://github.com/microsoft/typescript-go/issues/3909

function f(x: string | number) {
switch (typeof x) {
case "":
case "string":
x.charAt(0);
break;
}
}
Loading