所以,我有select
的接口。我需要有value
的类型。如果multiple
等于true
,value
类型必须是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.
1条答案
按热度按时间bkkx9g8r1#
Select
也应是泛型,并且它应将类型参数转发到SelectProps
。此外,multiple
的类型应为Multiple
运动场