接受原生组件属性作为 props,并在 React Native 中使用 TypeScript 接口自定义属性

You*_*mar 0 javascript typescript reactjs react-native

我有这个AppText组件,我想在其中使用Props接口,同时能够接受我可以通过spread operator. 但我收到这个错误:

Type '{ children: string; accessible: true; }' is not assignable to type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.Property 'accessible' does not exist on type 'IntrinsicAttributes & Props & { children?: ReactNode; }'
Run Code Online (Sandbox Code Playgroud)

应用文本:

Type '{ children: string; accessible: true; }' is not assignable to type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.Property 'accessible' does not exist on type 'IntrinsicAttributes & Props & { children?: ReactNode; }'
Run Code Online (Sandbox Code Playgroud)

我是如何使用它的:

<AppText accessible>Not a member yet ?</AppText> 
Run Code Online (Sandbox Code Playgroud)

小智 5

您应该能够重新定义Props,我假设TextProps可以从 导入react-native,如下所示:

interface Props extends TextProps {
  fontSize?: number;
  color?: string;
}
Run Code Online (Sandbox Code Playgroud)

这会增加TextProps您的属性,扩展运算符应该了解剩下的元素。