我有以下类型/接口:
type Nullable<T> = T | null;
export interface Employee {
name: string;
salary: number;
}
我不想将Employee的属性定义为可空,但整个Employees应该是可空的。
因为我不想每次使用都按Nullable<Employee>
的方式打字。我宁愿把它打成Employee
,然后它就会自动变成Employee | null
您需要一个类型别名:
export type Employee = {
name: string;
salary: number;
} | null