在我的 react native 应用程序中,我使用 typescript 。当我将 returnKeyType 作为 prop 传递时,我必须设置 returnKeyType 的类型。
<TextInput
editable={editable}
returnKeyType={returnKeyType}
maxLength={maxLength}
/>
我的界面是这样的。
interface IProps extends Props<IProps> {
editable?: boolean;
maxLength?: number;
returnKeyType: string;
}
当我将 returnKeyType 设置为字符串时,它会给出这样的错误。
"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}>"
如何在这里设置类型?
最佳答案
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";
}
关于typescript - React Native 中的 ReturnKeyType 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029336/