You can also use type for primitives with limited values. For example, if you have two different user types ('admin' and 'normal') you can write it down using type as follows:

type UserRole = 'admin' | 'normal';

interface User {

name:string;

age:number;

userRole:UserRole;

}

This prevents us from assigning the wrong value to our variables.

You can benefit from both of them like above and be as strict as it takes in your code.

Hossein Mousavi
Hossein Mousavi

Written by Hossein Mousavi

Senior Software Engineer @AIHR 🇳🇱 hossein.dev

Responses (2)