【问题标题】:React Native Custom TextInput with additional field in Typescript使用 Typescript 中的附加字段反应本机自定义 TextInput
【发布时间】:2021-07-29 08:17:42
【问题描述】:

我想构建一个组件,它采用Icon 参数并将所有其他参数传递给样式组件Input(基于TextInput)。在 Javascript 中,它非常简单:

import React from 'react';
import { TextInput } from 'react-native';
import styled from 'styled-components/native';

const Input = styled.TextInput`
  color: #268596;
`;

export default ({ Icon, ...props }) => (
  <InputArea>
    <Icon fill="#268596" />
    <Input {...props} />
  </InputArea>
);

但是,我想使用 Typescript(我是个初学者)。我尝试了以下方法。

import React from 'react';
import { TextInputProps, TextInput } from 'react-native';
import styled from 'styled-components/native';

const Input = styled.TextInput`
  color: #268596;
`;

type InputAreaProps = {
  Icon: React.FC<React.SVGAttributes<SVGElement>>;
} & TextInputProps;

export default ({ Icon, ...props }: InputAreaProps) => (
  <InputArea>
    <Icon fill="#268596" />
    <Input {...props} />
  </InputArea>
);

我得到了 TextInput 参数的所有智能感知和自动补全功能,但 TypeScript 并不满意。它一直在这里抱怨:

    <Input {...props} />
     ^^^^^

然后说:

No overload matches this call.
  Overload 1 of 2, '(props: Omit<Omit<TextInputProps & RefAttributes<TextInput>, never> & Partial<Pick<TextInputProps & RefAttributes<TextInput>, never>>, "theme"> & { ...; } & { ...; } & { ...; }): ReactElement<...>', gave the following error.
Type '{ allowFontScaling?: boolean | undefined; autoCapitalize?: "none" | "sentences" | "words" | "characters" | undefined; autoCorrect?: boolean | undefined; autoFocus?: boolean | undefined; ... 103 more ...; showSoftInputOnFocus?: boolean | undefined; }' is not assignable to type 'Omit<Omit<TextInputProps & RefAttributes<TextInput>, never> & Partial<Pick<TextInputProps & RefAttributes<TextInput>, never>>, "theme">'.
      Types of property 'style' are incompatible.
        Type 'import("/Projects/node_modules/@types/react-native/index").StyleProp<import("/Projects/node_modules/@types/react-native/index").TextStyle>' is not assignable to type 'import("/Projects/node_modules/@types/styled-components-react-native/node_modules/@types/react-native/index").StyleProp<import("/Projects/node_modules/@types/styled-components-react-native/node_modules/@types/react-native/index").TextStyle>'.
          Type 'TextStyle' is not assignable to type 'StyleProp<TextStyle>'.
            Type 'import("/Projects/node_modules/@types/react-native/index").TextStyle' is not assignable to type 'import("/Projects/node_modules/@types/styled-components-react-native/node_modules/@types/react-native/index").TextStyle'.
              Types of property 'color' are incompatible.
                Type 'import("/Projects/node_modules/@types/react-native/index").ColorValue | undefined' is not assignable to type 'import("/Projects/node_modules/@types/styled-components-react-native/node_modules/@types/react-native/index").ColorValue | undefined'.
                  Type 'unique symbol' is not assignable to type 'ColorValue | undefined'.
  Overload 2 of 2, '(props: StyledComponentPropsWithAs<typeof TextInput, DefaultTheme, {}, never, typeof TextInput>): ReactElement<StyledComponentPropsWithAs<typeof TextInput, DefaultTheme, {}, never, typeof TextInput>, string | JSXElementConstructor<...>>', gave the following error.
    Type '{ allowFontScaling?: boolean | undefined; autoCapitalize?: "none" | "sentences" | "words" | "characters" | undefined; autoCorrect?: boolean | undefined; autoFocus?: boolean | undefined; ... 103 more ...; showSoftInputOnFocus?: boolean | undefined; }' is not assignable to type 'Omit<Omit<TextInputProps & RefAttributes<TextInput>, never> & Partial<Pick<TextInputProps & RefAttributes<TextInput>, never>>, "theme">'.
      Types of property 'style' are incompatible.
        Type 'import("/Projects/node_modules/@types/react-native/index").StyleProp<import("/Projects/node_modules/@types/react-native/index").TextStyle>' is not assignable to type 'import("/Projects/node_modules/@types/styled-components-react-native/node_modules/@types/react-native/index").StyleProp<import("/Projects/node_modules/@types/styled-components-react-native/node_modules/@types/react-native/index").TextStyle>'

我能做些什么来让 TypeScript 开心?我在哪里可以尝试自己发现这些东西?

【问题讨论】:

    标签: typescript react-native styled-components


    【解决方案1】:

    您能否尝试将TextInput 包装在样式组件中,而不是使用点表示法。也许在某个地方与另一个 TextInput 有冲突。

    import { TextInputProps, TextInput } from "react-native";
    import styled from 'styled-components';
    
    const Input = styled(TextInput)` // using styled()
      color: #268596;
    `;
    
    type InputAreaProps = {
        Icon: React.FC<React.SVGAttributes<SVGElement>>;
    } & TextInputProps;
    
    export default ({ Icon, ...props }: InputAreaProps) => (
      <InputArea>
        <Icon fill="#268596" />
        <Input {...props} />
      </InputArea>
    );
    

    【讨论】:

    • 成功了,谢谢!但我不明白为什么它有效。
    • 我深入研究了代码,发现您应该使用styled-components-react-native,对吗? styled-components-react-native 包含它自己的 react-native 组件定义,它与 react-native 有冲突。您正在使用来自“react-native”的 TextInputProps,但 Input -> TextInputProps 使用的是 styled-components-react-native 之一。因此,类型不匹配
    • 使用 styled(TextInput) 将使用回react-native 类型定义,TextInputProps 也来自react-native 方面。所以,问题解决了
    • 我检查他们是否正在解决一些问题。也许有一个与您的案例有关github.com/DefinitelyTyped/DefinitelyTyped/pull/49914
    猜你喜欢
    • 2021-04-18
    • 1970-01-01
    • 2020-11-19
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2019-05-12
    相关资源
    最近更新 更多