我不明白为什么TS用这条消息抱怨return option[optionTextKey]
个:
Type TTextKey cannot be used to index type TOption
代码:
type Props<
TTextKey,
TOption extends (TTextKey extends string ? { [key in TTextKey]: string; } : string),
> = (
{
optionTextKey: TTextKey;
options: TOption[];
}
|
{
options: string[];
optionTextKey?: never;
}
) & {
name: string;
placeholder: string;
};
function LinkDropdown<
TTextKey,
TOption extends (TTextKey extends string ? { [key in TTextKey]: string; } : string),
>({
options,
optionTextKey,
name,
placeholder,
}: Props<TTextKey, TOption>): ReactElement
// some code
const getOptionText = (option: TOption, optionTextKey: TTextKey) => {
if ('string' === typeof option) return option;
// ts complains here
return option[optionTextKey];
};
// some code
}
如果我这样赚TOption
,这条消息就不存在了:
TOption extends TTextKey extends { [key in TTextKey]: string; }
upd个
我怎么才能修好它呢?