【发布时间】:2021-09-14 11:30:40
【问题描述】:
我有以下接口和使用它的 React Native 组件:
import { KeyboardTypeOptions, TextInputProps } from 'react-native';
import { TextInput } from 'react-native-paper';
export interface ValidatedInputProps {
autoCapitalize?: 'none' | 'sentences' | 'words' | 'characters',
contentType: Pick<TextInputProps, 'textContentType'>,
isSecure?: boolean,
keyboard?: KeyboardTypeOptions,
label: string,
}
export const ValidatedInput = ({
autoCapitalize = 'none',
contentType,
isSecure = false,
keyboard = 'default',
label,
}: ValidatedInputProps) => {
return (
<TextInput
textContentType={contentType} <-- Error is flagged here
...other props
/>
);
};
这让我在 VSCode 中弹出以下错误:
Type 'Pick<TextInputIOSProps, "textContentType">' is not assignable to type '"none" | "name" | "URL" | "addressCity" | "addressCityAndState" | "addressState" | "countryName" | "creditCardNumber" | "emailAddress" | "familyName" | "fullStreetAddress" | ... 17 more ... | undefined'.
Type 'Pick<TextInputIOSProps, "textContentType">' is not assignable to type '"oneTimeCode"'.ts(2322)
index.d.ts(1250, 5): The expected type comes from property 'textContentType' which is declared here on type 'IntrinsicAttributes & Pick<TextInputProps, "textContentType" | "clearButtonMode" | "clearTextOnFocus" | "dataDetectorTypes" | "enablesReturnKeyAutomatically" | ... 114 more ... | "dense"> & { ...; } & { ...; }'
(JSX attribute) textContentType?: "none" | "name" | "URL" | "addressCity" | "addressCityAndState" | "addressState" | "countryName" | "creditCardNumber" | "emailAddress" | "familyName" | "fullStreetAddress" | ... 17 more ... | undefined
我显然完全误解了Pick<T, K> 类型的观点,但我在过去两个小时内阅读的关于该主题的所有内容似乎都暗示我的用法是正确的。那我错过了什么?
我想我可以用正确的 union 定义我的 contentType 属性(就像我对 autoCapitalize 所做的那样),但是谁愿意用过长的字符串来膨胀他们的代码,当它已经存在时在别处定义?!我不能只导入该属性/类型并完成它吗??
【问题讨论】:
-
请与所有导入共享可重现的示例
-
我认为
ValidatedInputProps应该扩展Pick<TextInputIOSProps, 'textContentType'>,然后添加TextInputIOSProps接口上没有的额外props
标签: reactjs typescript react-native