说我有以下界面。
interface Client {
id: number;
email: string;
firstName: string;
lastName: string;
cellNumberFull: string;
}
我希望以下界面仅包含GoClient
中存在的属性。
interface ClientRestricted {
firstName: string;
lastName: string;
cellNumberFull: string;
foo: string; // This would throw an error
}
寻找延伸相反的国王。这样的东西存在吗?
答案 0 :(得分:1)
我倾向于让您的接口扩展一个映射的条件类型,这是它本身的功能。这是一个递归类型定义(称为F-bounded quantification),可让您执行一些相当强大的(如果造成混淆)类型约束。例如:
type Restrict<T, U> = { [K in keyof U]: K extends keyof T ? T[K] : never };
type RestrictClient<U> = Restrict<Client, U>;
// okay as desired
interface Okay extends RestrictClient<Okay> {
firstName: string;
lastName: string;
cellNumberFull: string;
}
// error, as desired
interface Extra extends RestrictClient<Extra> {
// ~~~~~
// Types of property 'foo' are incompatible.
// Type 'string' is not assignable to type 'never'.
firstName: string;
lastName: string;
cellNumberFull: string;
foo: string;
}
通过创建新界面I extends RestrictClient<I>
,当且仅当I
可分配给RestrictClient<I>
,即I
可分配给{[K in keyof I]: K extends keyof Client ? Client[K] : never}
时,此方法才有效,表示K
中的每个键I
以Client
出现并且是相同(或更窄)的类型。
这还会产生以下行为,这可能会或可能不会解决您的用例:
// okay to narrow properties
interface Narrowed extends RestrictClient<Narrowed> {
firstName: "specificString";
}
// error to widen properties
interface Widened extends RestrictClient<Widened> {
// ~~~~~~~ <-- number not assignable to string
firstName: string | number;
}
// error to change property to unrelated types
interface Unrelated extends RestrictClient<Unrelated> {
// ~~~~~~~~~ <-- number not assignable to string
firstName: number;
}
如果它与您要查找的内容不完全匹配,则可以更改Restrict
的定义以使其更紧密地对齐。无论如何,希望这能给您一些想法。祝你好运!