英文:
Typescript allow only one property
问题
This code allows me to send in props for just one of the options but not the other, for example:
<Drawer volunteer />
现在只有3个属性,但如果有更多属性,我应该如何更加通用地编写它?
抽屉的类型声明:
type DrawerTypes =
{ volunteer: boolean } & { rescues?: never } & { map?: never } |
{ volunteer?: never } & { rescues: boolean } & { map?: never } |
{ volunteer?: never } & { rescues?: never } & { map: boolean };
英文:
This code allow me to send in props just one of the options but not the other, for example:
<Drawer volunteer />
now it's 3 properties, but if there is more how can i write it more generic ?
The Drawer types declaration:
type DrawerTypes =
{ volunteer: boolean } & { rescues?: never } & { map?: never } |
{ volunteer?: never } & { rescues: boolean } & { map?: never } |
{ volunteer?: never } & { rescues?: never } & { map: boolean };
答案1
得分: 0
以下是您要翻译的内容:
可以使用映射类型来映射键,选择特定键并将其他键设置为 `never`:
```ts
type OneOf<T> = {
[K in keyof T]: Pick<T, K> & Partial<Record<Exclude<keyof T, K>, never>>;
}[keyof T]
新定义的 DrawerTypes
将是
type DrawerTypes = OneOf<{ volunteer: boolean; rescues: boolean; map: boolean }>;
诚然,DrawerTypes
的工具提示并不特别有帮助,所以如果您添加这个额外的部分,
type OneOf<T> = {
[K in keyof T]: Pick<T, K> & Partial<Record<Exclude<keyof T, K>, never>>;
}[keyof T] extends infer O ? { [K in keyof O]: O[K] } : never;
您可以看到 DrawerTypes
等同于
type DrawerTypes = {
volunteer: boolean;
rescues?: undefined;
map?: undefined;
} | {
rescues: boolean;
volunteer?: undefined;
map?: undefined;
} | {
map: boolean;
volunteer?: undefined;
rescues?: undefined;
}
这与您原始定义的 DrawerTypes
相同。
注意:`key?: undefined` 等同于没有 `exactOptionalPropertyTypes` 的 `key?: never`。
<details>
<summary>英文:</summary>
You can use a mapped type to map over the keys, picking the specific key and making the others `never`:
```ts
type OneOf<T> = {
[K in keyof T]: Pick<T, K> & Partial<Record<Exclude<keyof T, K>, never>>;
}[keyof T]
The new definition of DrawerTypes
would be
type DrawerTypes = OneOf<{ volunteer: boolean; rescues: boolean; map: boolean }>;
Admittedly, the tooltip of DrawerTypes
is not particularly helpful, so if you add this extra bit,
type OneOf<T> = {
[K in keyof T]: Pick<T, K> & Partial<Record<Exclude<keyof T, K>, never>>;
}[keyof T] extends infer O ? { [K in keyof O]: O[K] } : never;
you can see that DrawerTypes
is equivalent to
type DrawerTypes = {
volunteer: boolean;
rescues?: undefined;
map?: undefined;
} | {
rescues: boolean;
volunteer?: undefined;
map?: undefined;
} | {
map: boolean;
volunteer?: undefined;
rescues?: undefined;
}
which is the same as your original definition of DrawerTypes
.
note: key?: undefined
is equivalent to key?: never
without exactOptionalPropertyTypes
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论