react-native/Typescript: 如何将 autoComplete 值作为父组件 prop 传递给 TextInput?什么是正确的类型?

react-native/Typescript: how to pass autoComplete value to TextInput as the parent component prop? What's the correct type?

考虑一下我有一个像这样的自定义组件,里面有一个 TextInput(例如模态输入对话框):

interface Props {
    ...
    autoComplete: string;
}

const ModalInputDialog = (props: Props) => {

    ...

    return (<TextInput autoComplete={props.autoComplete} ... />);

};

Typescript 编译器抱怨 “没有重载匹配此调用”。问题是我不知道哪个是 autoComplete 属性 的正确类型。它不是字符串。

通过检查TextInput,似乎有一个大对象叫TextInputAndroidProps,但它也不起作用。

我要搜索的正确类型是什么?

您可以直接使用 TypeScript 从 TextInputAndroidProps 中提取类型,如下所示。

import { TextInput, TextInputAndroidProps } from "react-native";

interface Props {
    autoComplete: TextInputAndroidProps['autoComplete'];
}

在我目前的项目中,该道具仍然被称为 autoCompleteType,因此对于旧版本,这也是类似的。

import { TextInputAndroidProps } from "react-native";

interface Props {
    autoComplete: TextInputAndroidProps['autoCompleteType'];
}

您的项目中将需要 react-native types,但我想这很清楚。

另请注意,这是 android 唯一的道具。