Skip to content

TypeScript

TypeScript Casting: A Powerful Tool for Type Safety

TypeScript casting — or more accurately, type assertions — lets you tell the compiler "I know more about this type than you do." It doesn't convert data a...

23 Apr 2024 · 2 min read

TypeScript Casting: A Powerful Tool for Type Safety

TypeScript casting — or more accurately, type assertions — lets you tell the compiler "I know more about this type than you do." It doesn't convert data at runtime. It overrides the compiler's judgment at compile time.

That distinction matters. A lot.

The as syntax

The most common form. You assert that a value is a specific type:

Typescript
let value: any = 'hello';
let str: string = value as string;

This works because value is any. TypeScript trusts your assertion.

The angle bracket syntax

An older form that does the same thing:

Typescript
let value: any = 'hello';
let str: string = <string>value;

Avoid this in JSX/TSX files — it conflicts with JSX syntax. Stick with as.

When casting makes sense

Casting is most useful when TypeScript can't narrow a type on its own. Common scenarios:

DOM elements:

Typescript
const input = document.getElementById("search") as HTMLInputElement;
console.log(input.value);

TypeScript only knows getElementById returns HTMLElement | null. You know it's an input. The assertion bridges that gap.

API responses:

Typescript
interface User {
  name: string;
  email: string;
}

const data = await fetch("/api/user").then(r => r.json()) as User;

Type guards as an alternative:

Instead of casting, you can use a type guard for runtime safety:

Typescript
function isString(value: unknown): value is string {
  return typeof value === 'string';
}

let value: unknown = 'hello';
if (isString(value)) {
  console.log(value.toUpperCase()); // TypeScript knows it's a string
}

The trade-off

Casting lets you move fast when you know the type and TypeScript doesn't. That's the benefit.

The cost: you're bypassing the compiler. If you're wrong, TypeScript won't save you. The bug shows up at runtime, which is exactly what TypeScript is supposed to prevent.

My rule: use as sparingly. Prefer type guards when you can. And never cast to silence an error you don't understand — that's hiding a bug, not fixing one.

Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.

Go further
Live cohort on Maven

Forward Deployed AI Engineering: From Customer Problem to Production Value

A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.

January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.

View the live cohort

Keep reading