请考虑以下 TypeScript ( .tsx
) 代码:
import React from 'react';
import { TextInputProps } from 'react-native';
import { Container, TextInput, Icon } from './styles';
interface InputProps extends TextInputProps {
name: string;
icon: string;
}
const Input: React.FC<InputProps> = ({ name, icon, ...props }) => (
<Container>
<Icon name={icon} size={20} color="#666360" />
<TextInput
keyboardAppearance="dark"
placeholderTextColor="#666360"
{...props}
/>
</Container>
);
export default Input;
通过TextInputProps
作为类型参数传递给React.FC
我可以访问TextInput
我正在解构的属性...props
。但我也需要name
andicon
用于其他目的,所以我创建了一个扩展接口TextInputProps
,在那里指定了这些属性,然后传递InputProps
给React.FC
。
现在我得到'name' is missing in props validation - eslintreact/prop-types
了('icon' 相同),但是当我尝试获取内部指定的任何属性时,这并没有发生TextInputProps
。
写作const Input: React.FC<InputProps> = ({ name, icon, ...props }: InputProps) => (/*...*/);
使 linter 停止抱怨,但我仍然不明白为什么使用 type 参数不会。有人可以向我澄清这一点吗?我是不是搞错了一些概念,还是只是 linter 的问题?
PS:我正在用 ESLint 扩展在 VS Code 上写这篇文章。
PS2:这是里面的代码styles.ts
,如果有帮助的话:
import styled from 'styled-components/native';
import FeatherIcon from 'react-native-vector-icons/Feather';
export const Container = styled.View`
/* Some CSS */
`;
export const TextInput = styled.TextInput`
/* More CSS */
`;
export const Icon = styled(FeatherIcon)`
/* ... and CSS */
`;