# Assertion Functions (asserts)

> Permanently narrow a type for the rest of the scope block.

Subject: TypeScript | Lesson 16 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/assertion-functions
Previous: [Type Predicates (is)](https://codewithanimation.in/typescript/type-predicates.md) | Next: [Discriminated Unions](https://codewithanimation.in/typescript/discriminated-unions.md)

## The limitation of Type Guards

Standard type guards and type predicates require you to wrap your code inside an `if` block. This can lead to deep nesting and messy code.

An 'Assertion Function' does something different: it tells TypeScript that if the function returns normally (without throwing an error), the type is permanently narrowed for the remainder of the current scope. No `if` block required.

## Fail fast and keep code flat

This is a common pattern in testing (e.g. `assert(user !== null)`) and API validation.

By asserting conditions at the very top of a function and throwing an error if they fail, the rest of your function can proceed with a perfectly narrowed, safe type without any nesting.

## The 'asserts' keyword

```typescript
// Notice the return type:
function assertIsString(val: any): asserts val is string {
  if (typeof val !== "string") {
    throw new Error("Not a string!");
  }
}

function process(id: string | number) {
  assertIsString(id);
  
  // No if-block needed!
  // TS knows 'id' is a string for the rest of the function.
  console.log(id.toUpperCase());
}
```

## Common mistake: The silent failure trap

Just like Type Predicates (`is`), TypeScript blindly trusts the `asserts` keyword.

If you forget to actually `throw new Error()` inside your assertion function when the check fails, the function will return normally, TypeScript will assume the type is safe, and your code will crash on the next line.

## Tip: General assertions

You don't always have to narrow a specific parameter type. You can write a general assertion function like `function assert(condition: any, msg: string): asserts condition` that just guarantees a boolean expression was true.

## Try it: Try it

Watch how passing the `assertIsString` gatekeeper permanently locks the variable's type for the rest of the function scope.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/assertion-functions

## Check yourself

### Question 1

What is the primary difference between a Type Predicate ('val is string') and an Assertion Function ('asserts val is string')?

- A. Type Predicates narrow types inside an 'if' block; Assertion Functions narrow types for the entire remaining scope because they throw on failure.
- B. Assertion Functions can only be used on strings and numbers.
- C. Type Predicates are compiled to JavaScript, but Assertion Functions are erased.

Correct answer: A

Assertion functions act as a gatekeeper. If the execution passes the assertion function without an error being thrown, TypeScript knows it's safe to assume the narrowed type for the rest of the block.

- Why not B: They can be used on any type.
- Why not C: Both have their types erased, but the function logic itself remains in JS.

## Remember this

- Assertion functions use the `asserts val is Type` return signature.
- They narrow a type for the remainder of the scope, preventing nested `if` blocks.
- The function MUST throw an error if the condition fails.
- TypeScript trusts the signature blindly, so ensure your internal logic is flawless.
