Home > Enterprise >  interface as Nullable
interface as Nullable

Time:01-26

I've got the following types/interfaces:

type Nullable<T> = T | null;

export interface Employee {
  name: string;
  salary: number;
}

I don't want to define attributes of Employee to be Nullable, BUT the whole Employee should be Nullable.

Since I don't want do type everytime I use it as so Nullable<Employee>. I'd rather just type it as Employee which then would automatically be Employee | null

CodePudding user response:

You'll need a type alias :

export type Employee = {
  name: string;
  salary: number;
} | null
  •  Tags:  
  • Related