# Type Predicates (is)

> Teaching custom functions how to narrow types.

Subject: TypeScript | Lesson 15 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/type-predicates
Previous: [Control Flow & Type Guards](https://codewithanimation.in/typescript/control-flow-type-guards.md) | Next: [Assertion Functions (asserts)](https://codewithanimation.in/typescript/assertion-functions.md)

## The extraction problem

In the previous concept, we saw that extracting a type guard (`typeof val === "string"`) into a helper function breaks TypeScript's control flow analysis. A function that returns a `boolean` just means 'true or false'; it doesn't mean 'this specific variable is a string'.

A Type Predicate is a special return type that tells the compiler: 'If this function returns true, the argument passed to it is definitely this specific type'.

## Reusable type guards

When checking for complex shapes (like validating if an API response matches a specific interface), the check can be dozens of lines long.

Type predicates allow you to encapsulate that ugly logic into a clean, reusable helper function without losing type safety.

## The 'is' keyword

You define a type predicate by replacing the `boolean` return type with `parameterName is Type`.

```typescript
// Without predicate: returns boolean
// With predicate: returns val is string
function isString(val: any): val is string {
  return typeof val === "string";
}

if (isString(id)) {
  // TS now knows id is a string!
  id.toUpperCase();
}
```

## Common mistake: The danger of trusting predicates

TypeScript blindly trusts your type predicate. It does not check if the internal logic actually proves the type.

If you write `function isString(val: any): val is string { return true; }`, TypeScript will believe you. If you pass a number into it, the compiler will allow string methods, and your app will crash at runtime. Treat predicates with the same caution as the `!` operator.

## Tip: Use built-in guards when possible

Because predicates can lie, it's safer to use built-in type guards (like `in`, `typeof`, `instanceof`) directly in your `if` statements whenever possible, as the compiler can verify them automatically.

## Try it: Try it

Watch how changing a return type from `boolean` to `val is string` instantly fixes the compiler error inside the if-block.

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

## Check yourself

### Question 1

What is the primary risk of writing a custom Type Predicate?

- A. It slows down the compilation time significantly.
- B. TypeScript trusts your return type without verifying the internal logic, meaning you can easily create runtime crashes if your logic is wrong.
- C. It generates a lot of unnecessary JavaScript in the final bundle.

Correct answer: B

Type Predicates are essentially an 'escape hatch'. You are telling the compiler to stop analyzing and just trust you.

- Why not A: It has a negligible impact on compile time.
- Why not C: Like most TS features, it is erased at compile time.

## Remember this

- A regular function returning `boolean` cannot narrow a type in an `if` block.
- A Type Predicate (`val is string`) tells the compiler to narrow the type if it returns true.
- TypeScript blindly trusts type predicates; if your logic is wrong, your app will crash.
- Use them only for complex validations you need to reuse.
