英文:
Conditional Type on props value
问题
抱歉,代码部分不会被翻译。以下是翻译好的部分:
"Hi everyone I'm trying to create an interface for my function. The objective is that if the value of action is edit I want to have an indexToEdit else this parameter should be undefined or not mentioned. I don't think it's possible the way I tried it.
It's why I tried another solution.
I would like to find a clean way to do it.
Thanks for your answers."
英文:
Hi everyone I'm trying to create an interface for my function
<!-- language: lang-ts -->
interface IManageActions {
action: "add" | "remove" | "edit";
element: AddExtra;
list: AddExtra[];
indextoEdit: typeof action extends "edit" ? number : undefined;
}
The objectif is that if the value of action is edit I want to have a indexToEdit else this parameter should be undefind or not mention. I don't think it's possible the way I tried it.
it's why I tried an other solution.
<!-- language: lang-ts -->
interface IManageActions {
action: "add" | "remove" | "edit";
element: AddExtra;
list: AddExtra[];
}
interface IManageActionsEdit {
action: "edit";
element: AddExtra;
list: AddExtra[];
indextoEdit: number;
}
function manageActions({
action,
element,
list,
indextoEdit,
}: IManageActions | IManageActionsEdit){}
but now the following error occured.
> Property 'indextoEdit' does not exist on type 'IManageActions | IManageActionsEdit
I would like to find a clean way to do it.
thanks for your answers
答案1
得分: 0
以下是您要翻译的内容:
您正在寻找一个[distributive union type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types)。让我们首先提取共同的字段:
type CommonFields = { element: AddExtra; list: AddExtra[] }
现在,让我们创建整个类型,在 action: 'edit'
时才提到 indexToEdit
:
type IManageActions = CommonFields & (
| {
action: 'edit';
indexToEdit: number;
}
| {
action: 'add' | 'remove';
}
)
然而,这种方式仍然无法访问 indexToEdit
,因为它在变体中不存在。解决方法是将 indexToEdit
添加到另一种变体中,并将其设置为类型 never
的可选项:
type AddExtra = {};
type CommonFields = { element: AddExtra; list: AddExtra[] }
type IManageActions = CommonFields & (
| {
action: 'edit';
indexToEdit: number;
}
| {
action: 'add' | 'remove';
indexToEdit?: never;
}
);
<details>
<summary>英文:</summary>
You are looking for a [distributive union type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types). Let's extract the common fields first:
type CommonFields = { element: AddExtra; list: AddExtra[] }
Now, let's create the whole type, where the `indexToEdit` will be only mentioned where the `action: 'edit'`:
type IManageActions = CommonFields & (
| {
action: 'edit';
indextoEdit: number;
}
| {
action: 'add' | 'remove';
}
)
However, this way `indextoEdit` still won't be accessible because it doesn't exist in the variations. The solution would be to add the `indexToEdit` to the other variant and make it optional with of type `never`:
type AddExtra = {};
type CommonFields = { element: AddExtra; list: AddExtra[] }
type IManageActions = CommonFields & (
| {
action: 'edit';
indextoEdit: number;
}
| {
action: 'add' | 'remove';
indextoEdit?: never;
}
);
[playground](https://www.typescriptlang.org/play?ts=5.0.4#code/C4TwDgpgBAggJnAogD2AJwIZQLxQN4C+A3AFAmiRQDCA9gLZ00B2AYgJYQA2cAzjvlC4Q6EJsABcsBCnQYiUTmx4SpSVJgDaAXSgEyFaAEkAshiYYA5hBgBjYG2Z9ctBs3ZdeUAGRQAFCSgoAB98AMCoDDsHJkkAcgg4NmBY0nCoNiY4CFQaRESVJgBXOgAjCDRU3TCQvDDAyPtmOIwEWOCoWLRhGgA3CBS69Mzs4Fz8gH5JJgg+irC9AEpSIA)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论