More secure TypeScript code - easily

172 words | ~1 min
typescript javascript

How to secure JS object returned by API as a correct Typescript type?

TS type predicate function helps here. (and it’s not the only way)

Predicate function

First, let’s check the predicate function:

  type MyType = {type: 'MY_TYPE'}
  function isMyType(value: unknown): value is MyType {
    return value?.type === 'MY_TYPE';

How to use it?

  const someVal: unknown;

    const result: MyType = someVal

This check will work, because:

  • TS now knows that condition
  • isMyType passes MyType items

Want event more secure way? Use asserts

Asserts come with help:

  function assertIsMyType(value: any): assert value is MyType {
    if(value?.type !== 'MY_TYPE'){
      throw new AssertionError('Not MyType');

  const result: MyType = someVal

(see no if condition here?)

Why is it more secure?

Instead of a simple bool value return, assertions throws an error when the condition doesn’t pass.

It’s a fail fast approach that will notify you as soon the function fails to validate type. Besides, you write less ‘if’. Code is more straightforward