typescript :如何使上瘾从一个领域到另一个?

fzsnzjdm  于 2023-02-05  发布在  TypeScript
关注(0)|答案(1)|浏览(106)

所以,我有select的接口。我需要有value的类型。如果multiple等于truevalue类型必须是string[]string,如果multiple被禁用。

interface SelectProps {
  multiple?: boolean;
  value: string | string[];
}

组件:

const Select = (props: SelectProps) => {
   // code
};

我试着用普通的,像这样

interface SelectProps<Multiple extends boolean | undefined> {
  multiple?: boolean;
  value: Multiple extends true ? string[] : string;
}

const Select = (props: SelectProps<typeof props.multiple>) => {
   // code
};

但我收到错误:'multiple' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

bkkx9g8r

bkkx9g8r1#

Select也应是泛型,并且它应将类型参数转发到SelectProps。此外,multiple的类型应为Multiple
运动场

相关问题