条件类型基于props值

huangapple go评论60阅读模式
英文:

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: &quot;add&quot; | &quot;remove&quot; | &quot;edit&quot;;
  element: AddExtra;
  list: AddExtra[];
  indextoEdit: typeof action extends &quot;edit&quot; ? 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: &quot;add&quot; | &quot;remove&quot; | &quot;edit&quot;;
  element: AddExtra;
  list: AddExtra[];
}
interface IManageActionsEdit {
  action: &quot;edit&quot;;
  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;
  }
);

playground




<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&#39;s extract the common fields first:

type CommonFields = { element: AddExtra; list: AddExtra[] }


Now, let&#39;s create the whole type, where the `indexToEdit` will be only mentioned where the `action: &#39;edit&#39;`:

type IManageActions = CommonFields & (
| {
action: 'edit';
indextoEdit: number;
}
| {
action: 'add' | 'remove';
}
)


However, this way `indextoEdit` still won&#39;t be accessible because it doesn&#39;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>



huangapple
  • 本文由 发表于 2023年7月31日 23:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定