React Native中的ReturnKeyType数据类型

时间:2019-07-14 16:46:43

标签: typescript react-native

在我的本机应用程序中,我使用打字稿。当我将returnKeyType作为道具传递时,我必须设置returnKeyType的类型。

<TextInput
 editable={editable}
 returnKeyType={returnKeyType}
 maxLength={maxLength}
/>

我的界面就是这样。

interface IProps extends Props<IProps> {
  editable?: boolean;
  maxLength?: number;
  returnKeyType: string;
}

当我将returnKeyType设置为string时,会出现这样的错误。

"Type 'string' is not assignable to type '"none" | "done" | "search" | "default" | "go" | "next" | "send" | "previous" | "google" | "join" | "route" | "yahoo" | "emergency-call" | undefined'.ts(2322)
index.d.ts(1438, 5): The expected type comes from property 'returnKeyType' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<TextInput> & Readonly<TextInputProps> & Readonly<{ children?: ReactNode}>"

如何在此处设置类型?

1 个答案:

答案 0 :(得分:1)

returnKeyType是一个枚举,因此您应该能够执行以下操作:

interface IProps extends Props<IProps> {
  editable?: boolean;
  maxLength?: number;
  returnKeyType: "none" | "done" | "search" | "default" | "go" | "next" | "send" | "previous" | "google" | "join" | "route" | "yahoo" | "emergency-call";
}