如何在接口和类中强制自定义声明的类型?

时间:2019-12-07 07:56:48

标签: typescript types interface type-declaration

我创建了我的自定义类型,实际上是一个命名的原始类型,目的是为其中的数字或名称类型提供清晰的定义。例如:

export declare type AmountOfCredits = number;
export declare type AmountOfMoney = number;
export declare type AmountOfProduct = number;
export declare type BankAccountNumber = string;
export declare type CompanyName = string;
export declare type CountryName = string;
export declare type DayOfWeek = 0 | 1 | 2 | 3 | 4 | 5 | 6;
export declare type Days = number;
export declare type ID = number;
export declare type Month = number;
// ...etc

现在我可以使用此界面:

export interface EmployeeInterface {
  id: ID;
  user_id: ID;
  mother_name: PersonName;
  place_of_birth: SettlementName;
  date_of_birth: ISODate;
  name: PersonName;
}

但是在课堂上,这也是可行的:

export class Employee implements EmployeeInterface {
  id: number;
  user_id: number;
  mother_name: string;
  place_of_birth: string;
  date_of_birth: string;
  name: string;
}

我可以以某种方式强迫类仅接受自定义声明的类型,而不接受numberstring基本类型吗?

0 个答案:

没有答案