日本語

Fixing TS2367 “This comparison appears to be unintentional” in TypeScript

​​TypeScript​Troubleshooting

Comparing a value whose type is a union of string literals, such as "draft" | "published", with a string outside that union, such as status === "archived", produces error TS2367: This comparison appears to be unintentional because the types ... have no overlap. The error means that the two compared types share no value at all. You can make the error go away by adding the string to the union, or by fixing how the type is declared or narrowed.

This error appears in if code that branches on values of a union type (a string literal type) such as post status, button kind, or HTTP method, where only a fixed set of strings is allowed.

I reproduced this error in my test environment with both tsc and the TypeScript language server, and confirmed the full message and that the fixes below remove it.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, and TypeScript 7.0.2. To compare, I also ran every file in this article through tsc from TypeScript 5.9.3. The tsconfig.json was generated with npx tsc --init (it includes "strict": true). For the errors shown in editors such as VS Code, I did not test the editor itself. Instead, I ran the TypeScript 7 language server (tsc --lsp --stdio), which editors use through LSP (Language Server Protocol), directly and checked its output.

The full error

In src/bad.ts, I wrote a function that takes a status (draft or published) and returns a different label only for archived. The Status type does not include archived.

ts
type Status = "draft" | "published";

export function getLabel(status: Status) {
  if (status === "archived") {
    return "アーカイブ";
  }
  return status === "draft" ? "下書き" : "公開";
}

console.log(getLabel("draft"));

Running npx tsc --noEmit produced the following (exit code 1):

text
src/bad.ts(4,7): error TS2367: This comparison appears to be unintentional because the types 'Status' and '"archived"' have no overlap.

The message means: "This comparison appears to be unintentional. The Status type and the "archived" type have no overlap (no value that both can hold)." With --pretty, the whole status === "archived" expression was underlined (I removed the color control characters):

text
src/bad.ts:4:7 - error TS2367: This comparison appears to be unintentional because the types 'Status' and '"archived"' have no overlap.

4   if (status === "archived") {
        ~~~~~~~~~~~~~~~~~~~~~


Found 1 error in src/bad.ts:4

The language server reports the same error

TypeScript 7's tsc runs as a language server when started with --lsp --stdio. I wrote a small script that opened src/bad.ts in this server using the LSP sequence (initialize, then didOpen), and printed the diagnostics it returned along with the hover (type information) at status on line 4. The output was:

text
server: typescript-go 7.0.2
diagnostics (1):
  4:7-4:28 severity=1 source=ts code=2367 This comparison appears to be unintentional because the types 'Status' and '"archived"' have no overlap.
hover 4:7: (parameter) status: Status

severity=1 is the value that the LSP specification uses for errors. The message is the same as in tsc, and the type of status is still Status.

Why TypeScript says there is no overlap

The type of status is "draft" | "published", so it can only hold draft or published. Both of these strings differ from "archived", so status === "archived" is always false. TypeScript reports a comparison as TS2367 when the two sides of === have no common value, because this usually indicates a mistake. The Handbook explains unions of specific values like "draft" | "published" as literal types (in English).

Even with the error, tsc still emitted JavaScript. It kept status === "archived" in the output as a comparison that is always false, so running getLabel("draft") with Node.js printed 下書き. TS2367 does not mean the code cannot run. It warns that the comparison is meaningless (always false) and may be a mistake.

If the compared string is correct, add it to the union

If archived really is a status, add it to the Status type:

diff
-type Status = "draft" | "published";
+type Status = "draft" | "published" | "archived";

I also changed the last call to getLabel("archived").

When I ran npx tsc --noEmit on the fixed file, it printed nothing and exited with code 0. Running the emitted JavaScript printed アーカイブ. This is the fix when the type definition lists fewer states than the code actually uses.

Strings from outside: receive them as string, then narrow

The opposite case also happens: Status is correct, and the code that compares against an impossible value like archived is the mistake. The following src/or-bad.ts tries to reject values that are neither draft nor published by using || (or), but its second comparison gets TS2367:

ts
type Status = "draft" | "published";

export function check(status: Status) {
  if (status !== "draft" || status !== "published") {
    throw new Error(`不明な状態: ${status}`);
  }
  return status;
}

console.log(check("draft"));
text
src/or-bad.ts(4,29): error TS2367: This comparison appears to be unintentional because the types '"draft"' and '"published"' have no overlap.

In the language server, hovering over the second status on line 4 showed that its type had been narrowed to "draft":

text
hover 4:29: (parameter) status: "draft"

The right side of || is evaluated only when the left side is false. When status !== "draft" is false, status must be "draft", so on the right side the type is narrowed to "draft". Comparing that "draft" with "published" has no overlap. Also, || is true when either side is true: a draft value satisfies the right-hand condition, and a published value satisfies the left-hand one, so every possible value enters this if block.

If the real goal is to reject strings that are not in Status, receive the argument as string and narrow it with && (and) before returning:

ts
type Status = "draft" | "published";

export function toStatus(value: string): Status {
  if (value !== "draft" && value !== "published") {
    throw new Error(`不明な状態: ${value}`);
  }
  return value;
}

console.log(toStatus("draft"));
try {
  toStatus("archived");
} catch (error) {
  console.log((error as Error).message);
}

Because the argument is string, both value !== "draft" and value !== "published" compare types that overlap, so TS2367 does not appear. && rejects only the values that satisfy both conditions (values that are neither). After the throw, value is narrowed to "draft" | "published", so it can be returned as Status. This narrowing works as described in the Handbook's section on equality narrowing (in English). When I ran the emitted JavaScript, it printed:

text
draft
不明な状態: archived

Comparing again after narrowing

After an if has removed a value, comparing again with that same value also produces TS2367. In src/narrowed.ts below, the second if is always false:

ts
type Status = "draft" | "published";

export function getLabel(status: Status) {
  if (status === "draft") {
    return "下書き";
  }
  if (status === "draft") {
    return "下書き(2回目)";
  }
  return "公開";
}

console.log(getLabel("published"));
text
src/narrowed.ts(7,7): error TS2367: This comparison appears to be unintentional because the types '"published"' and '"draft"' have no overlap.

The first if (status === "draft") returns, so after it status is narrowed to "published" only. Hovering over status on line 7 in the language server showed (parameter) status: "published". Comparing "published" with "draft" has no overlap, so the second comparison is an error. When I removed the duplicate second if, tsc stopped reporting the error, and running getLabel("published") printed 公開.

The same error with a number compared to a string

TS2367 is not limited to string literal types. It also appears whenever two types with no overlap are compared. In src/number-bad.ts, which compares a number value with a string, I got the same error:

ts
export function isFirst(id: number) {
  return id === "1";
}

console.log(isFirst(1));
text
src/number-bad.ts(2,10): error TS2367: This comparison appears to be unintentional because the types 'number' and 'string' have no overlap.

id is number and "1" is string, and === is always false when the types differ. If you mix up a value that arrives as a string, such as a query parameter, with a value you hold as a number, this error helps you catch it. Changing the comparison to id === 1, which compares number with number, removed the error.

With TypeScript 5.9.3's tsc, the four errors in this article appeared at the same positions with the same text, and none of the four fixed files produced an error.

​Posted by 86