简体   繁体   English

react-select typescript 问题 - 通用类型“ValueType”需要 2 个类型参数。ts(2314)

[英]react-select typescript issue - Generic type 'ValueType' requires 2 type argument(s).ts(2314)

I am trying to utilize react-select and have the following code:我正在尝试利用react-select并具有以下代码:

import React, {useState} from 'react';
import LanguageChange from '../../../Icons/LanguageChange';
import Select, { ValueType } from 'react-select';

type OptionType = {
    value: string;
    label: string;
  };


export const LanguageSelector = () => {
    const languageOptions: OptionType[] = [
        { value: 'English', label: 'EN' },
        { value: 'German', label: 'DE' },
        { value: 'French', label: 'FR' },
      ];
    const [selectedOption, setSelectedOption] = useState<ValueType<OptionType>>(languageOptions[0]);
    const handleChange = (option: ValueType<OptionType>) => {
    setSelectedOption(option);
  };

    return (
        <LanguageChange>
            <Select
                value={selectedOption}
                onChange={setSelectedOption}
                options={languageOptions}
            />
        </LanguageChange>
    )
}

But I keep getting the following error: (alias) type ValueType<OptionType extends OptionTypeBase, IsMulti extends boolean> = IsMulti extends true?但我不断收到以下错误:(别名)类型 ValueType<OptionType extends OptionTypeBase, IsMulti extends boolean> = IsMulti extends true? OptionsType: OptionType |选项类型:选项类型 | null import ValueType Generic type 'ValueType' requires 2 type argument(s).ts(2314) null 导入 ValueType 通用类型“ValueType”需要 2 个类型参数。ts(2314)

Any idea what I'm missing here?知道我在这里缺少什么吗?

ValueType requires two generics: the OptionType which you have provided and IsMulti which is either true or false . ValueType需要两个 generics:您提供的OptionTypeIsMulti ,它是truefalse IsMulti determines whether or not it is an array. IsMulti确定它是否是一个数组。 If you have an OptionType with {value: string} , the ValueType if it's a multiple select should be string[] whereas for a single select it would be string .如果您有一个带有{value: string}OptionType ,则ValueType如果它是多个 select 应该是string[]而对于单个 select 它应该是string

In your case you have a single select so you can use ValueType<OptionType, false> .在您的情况下,您有一个 select ,因此您可以使用ValueType<OptionType, false> But you can also just use string because you already know that the value type of your options is string so there is no need to work backwards.但是您也可以只使用string ,因为您已经知道选项的值类型是string ,因此无需向后工作。

Source Code 源代码

Update your onChange function to accept params like this:更新您的 onChange function 以接受如下参数:

onChange: (value: ValueType<OptionType, IsMulti>, actionMeta: ActionMeta<OptionType>) => void;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通用类型“ReturnType”需要 1 个类型参数。ts(2314) - Generic type 'ReturnType' requires 1 type argument(s).ts(2314) 错误TS2314:通用类型&#39;组件 <P, S> &#39;需要2种类型的参数 - error TS2314: Generic type 'Component<P, S>' requires 2 type argument(s) 反应选择和打字稿:类型“字符串”不可分配给类型“ValueType”<OptionTypeBase> &#39; - react-select and typescript: Type 'string' is not assignable to type 'ValueType<OptionTypeBase>' React 中的错误:通用类型“DatePickerProps”需要 1 个类型参数 - Error in React: Generic type 'DatePickerProps' requires 1 type argument(s) 通用类型'FlatListProps<itemt> ' 需要 1 个类型参数</itemt> - Generic type 'FlatListProps<ItemT>' requires 1 type argument(s) 使用打字稿为键选择通用类型 - React select generic type for key with typescript 反应选择正确的 onchange 值类型 - React-select correct onchange value type 通过 typescript 中函数的可选参数返回不同的泛型类型 - return different generic type via function's optional argument in typescript 反应选择打字​​稿问题(反应使用形式状态) - react-select typescript issue (react-use-form-state) next-redux-wrapper 通用类型“MakeStore”在 configureStore 中需要 1 个类型参数 - next-redux-wrapper Generic type 'MakeStore' requires 1 type argument(s) in configureStore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM