Syntax

TypeScript Narrow with typeof Syntax

In TypeScript, narrow with typeof: narrows a union with a typeof check — if (typeof id === "string") { ... }

Quick Answer

Syntax
if (typeof id === "string") { ... }
TypeScriptSyntax

Narrow with typeof

Code example
if (typeof id === "string") { ... }

Narrows a union with a typeof check.

Learn More

How to narrow with typeof in TypeScript

  1. Open your TypeScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

TypeScript Narrow with typeof Syntax

Use if (typeof id === "string") { ... }.

Related Syntax References

Frequently asked questions

What is the TypeScript syntax for narrow with typeof?

Use this pattern: if (typeof id === "string") { ... }

Can I copy this TypeScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this TypeScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify