# Type Assertions & Non-Null

> What are type assertions in TypeScript? When should you use them and what are the risks?

Canonical: https://www.internhack.in/learn/interview/typescript-interview/ts-type-assertions
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

What are type assertions in TypeScript? When should you use them and what are the risks?

## Answer

Type assertions tell TypeScript 'trust me, I know the type'. They override the compiler's inferred type without any runtime check. TypeScript uses the 'as' syntax (or angle-bracket syntax in non-JSX files).

Key assertion types:

- 'as Type': Override the inferred type (most common)
- '!' (non-null assertion): Tell TS a value is not null/undefined
- 'as const': Narrow to the most specific literal type
- 'satisfies': Validate a type while keeping the narrow inferred type

Assertions are NOT type casts - they perform zero runtime conversion. They simply tell the compiler to treat a value as a different type. Incorrect assertions lead to runtime errors.

## Code examples

### Type Assertions with 'as'

```
// Basic assertion
const input = document.getElementById('username') as HTMLInputElement;
input.value = 'Alice'; // OK: TypeScript trusts our assertion

// Without assertion: getElementById returns HTMLElement | null
const generic = document.getElementById('username');
// generic.value; // Error: Property 'value' does not exist on HTMLElement

// Asserting API responses
interface User {
  id: number;
  name: string;
  email: string;
}

async function fetchUser(): Promise<User> {
  const response = await fetch('/api/user');
  const data = await response.json(); // returns 'any'
  return data as User; // Assert the shape
}

// Double assertion (escape hatch for incompatible types)
const value = 'hello' as unknown as number;
// DON'T do this unless you truly know what you're doing

// Angle-bracket syntax (not in JSX files)
const el = <HTMLInputElement>document.getElementById('input');
```

Type assertions override the compiler but add zero runtime safety. Use them sparingly and only when you have knowledge the compiler lacks.

### Non-Null Assertion (!)

```
// Non-null assertion operator
function getLength(value: string | null): number {
  // value!.length tells TS: 'value is definitely not null'
  return value!.length; // Risky! Could crash if null
}

// Better: use narrowing instead
function getLengthSafe(value: string | null): number {
  if (value === null) throw new Error('Value is null');
  return value.length; // TS narrows automatically
}

// Common use case: DOM elements you know exist
const app = document.getElementById('app')!;
// Without !: app is HTMLElement | null
// With !: app is HTMLElement

// Optional chaining is often safer
const maybeEl = document.getElementById('maybe');
const text = maybeEl?.textContent ?? 'default';

// Definite assignment assertion
class Component {
  name!: string; // Tells TS: I'll assign this before use
  
  constructor() {
    this.initialize();
  }
  
  initialize() {
    this.name = 'MyComponent';
  }
}
```

The ! operator suppresses null/undefined checks. Prefer narrowing (if checks) or optional chaining (?.) over non-null assertions for safer code.

### satisfies Operator (TS 4.9+)

```
// Problem: explicit type annotation widens the type
type Colors = Record<string, [number, number, number] | string>;

const colorsAnnotated: Colors = {
  red: [255, 0, 0],
  green: '#00ff00',
  blue: [0, 0, 255],
};
// colorsAnnotated.red.map(...) // Error! Could be string

// satisfies: validates the type but keeps narrow inference
const colorsSatisfies = {
  red: [255, 0, 0],
  green: '#00ff00',
  blue: [0, 0, 255],
} satisfies Colors;

colorsSatisfies.red.map(v => v / 255);  // OK! TS knows it's a tuple
colorsSatisfies.green.toUpperCase();     // OK! TS knows it's a string

// satisfies + as const
const routes = {
  home: '/',
  about: '/about',
  contact: '/contact',
} as const satisfies Record<string, string>;

// Validates shape AND keeps literal types
type Route = typeof routes[keyof typeof routes];
// '/' | '/about' | '/contact'
```

satisfies validates that a value matches a type without widening it. Combined with as const, it gives you both validation and narrow literal types.

## Key points

- Assertions are compile-time only - they add zero runtime safety
- Prefer narrowing (if/typeof/instanceof) over assertions whenever possible
- satisfies is the modern best practice: validates without widening

## Common follow-up questions

- When is a double assertion (as unknown as T) acceptable?
- How does satisfies differ from a type annotation?
- What are the risks of non-null assertions in production code?

## Interview tips

- Emphasize that assertions are NOT runtime casts - this is a common misconception
- Show satisfies as a modern alternative - it demonstrates up-to-date TS knowledge
- Mention that excessive assertions often indicate a design problem

## Concepts covered

Type Assertions, as keyword, Non-Null Assertion, const Assertion, satisfies Operator

Previous: [unknown vs any vs never](https://www.internhack.in/learn/interview/typescript-interview/ts-unknown-any-never) · Next: [Discriminated Unions](https://www.internhack.in/learn/interview/typescript-interview/ts-discriminated-unions) · [All of TypeScript Interview](https://www.internhack.in/learn/interview/typescript-interview)

## More TypeScript Interview questions

- [Mapped Types](https://www.internhack.in/learn/interview/typescript-interview/ts-mapped-types)
- [Conditional Types & infer](https://www.internhack.in/learn/interview/typescript-interview/ts-conditional-types)
- [Template Literal Types](https://www.internhack.in/learn/interview/typescript-interview/ts-template-literal)
- [Utility Types (Partial, Pick, etc.)](https://www.internhack.in/learn/interview/typescript-interview/ts-utility-types)
- [keyof & typeof Operators](https://www.internhack.in/learn/interview/typescript-interview/ts-keyof-typeof)
- [Index Signatures & Record](https://www.internhack.in/learn/interview/typescript-interview/ts-index-signatures)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [TypeScript Interview](https://www.internhack.in/learn/interview/typescript-interview)
