简体   繁体   English

Typescript 在 React 功能组件(React.FC<props> ) 错误的道具是作为道具发送的吗?</props>

[英]Typescript isn't complaining when a React Functional Component (React.FC<Props>) with wrong props is send as a prop?

I am trying to send a functional component with some props to another component and in the receiving component trying to type check that component for some of the props.我正在尝试将带有一些道具的功能组件发送到另一个组件,并在接收组件中尝试键入检查该组件的某些道具。 Here is the code:这是代码:

// BaseButton.tsx

export type ButtonProps = {
  label: string
  size?: 'sm' | 'md' | 'lg' | 'normal'
  icon?: React.FC<{ size?: number; foo: string }>
} & React.ButtonHTMLAttributes<HTMLButtonElement

export const BaseButton: React.FC<ButtonProps> = props => {/* ...return the button component */}
// Icon.tsx

type Props = {
  style?: React.CSSProperties
  size?: number
}

export const Icon: React.FC<Props> = ({ style, size = 24 }) => {/* ... return Icon component */}
// where I render the component

<BaseButton icon={Icon} label="Sign in" />
//                ^^^ I am expecting Typescript to complain

Since the icon?: React.FC<{ size?: number; foo: string }>icon?: React.FC<{ size?: number; foo: string }> icon?: React.FC<{ size?: number; foo: string }> in ButtonProps has an extra prop foo I am expecting Typescript to complain about it. icon?: React.FC<{ size?: number; foo: string }> in ButtonProps有一个额外的道具foo我期待 Typescript 抱怨它。 But it is not.但事实并非如此。

EDIT:编辑:

This will throw an error while trying to invoke it with JSX syntax:这将在尝试使用 JSX 语法调用它时引发错误:

icon?: React.FC<{ size?: number; foo: string }>
const {icon: Icon} = props
<Icon />

Instead, the type should be:相反,类型应该是:

icon?: React.ComponentType<{ bar: number }>

Basically, you can assign functions with fewer parameters to functions with more parameters :基本上,您可以将具有较少参数的函数分配给具有更多参数的函数

let fn1 = (props: { size: number }) => 42
let fn2 = (props: { size: number; foo: string }) => 42

fn2 = fn1  // this works
fn1 = fn2 // error

Or in other words:或者换句话说:

With a variable let fn2 and type (props: { size: number; foo: string }) => number given, a client needs to call it with two props size and foo .使用变量let fn2和类型(props: { size: number; foo: string }) => number given,客户端需要使用两个 props sizefoo调用它。 We now can assign fn2 = fn1 , as it is OK for the underlying implementation fn1 to ignore some of the input props and only deal with size .我们现在可以分配fn2 = fn1 ,因为底层实现fn1可以忽略一些输入道具并且只处理size

Carried over to your case:转移到您的案例中:

Icon with type React.FC<{ style?: React.CSSProperties; size?: number}>类型为React.FC<{ style?: React.CSSProperties; size?: number}>Icon React.FC<{ style?: React.CSSProperties; size?: number}> is assignable to the ButtonProps icon property with type React.FC<{ size?: number; foo: string }> React.FC<{ style?: React.CSSProperties; size?: number}>可分配给类型为React.FC<{ size?: number; foo: string }>ButtonProps图标属性React.FC<{ size?: number; foo: string }> . React.FC<{ size?: number; foo: string }>

let fc1: React.FC<{
  style?: React.CSSProperties
  size?: number
}> = // ...
let fc2: React.FC<{ size?: number; foo: string }> = // ...

fc2 = fc1 // works
fc1 = fc2 // error

Playground 操场

The type FunctionComponent has function type definition, so you can pass either a React component or a function to be invoked later. FunctionComponent类型具有 function 类型定义,因此您可以传递 React 组件或 function 以供稍后调用。

type FC<P = {}> = FunctionComponent<P>;

interface FunctionComponent<P = {}> {
    (props: PropsWithChildren<P>, context?: any): ReactElement | null; <--- Here
    propTypes?: WeakValidationMap<P>;
    contextTypes?: ValidationMap<any>;
    defaultProps?: Partial<P>;
    displayName?: string;
}

Here's a sandbox with an example of invoking the props.icon component from the BaseButton component.这是一个沙箱,其中包含从 BaseButton 组件调用props.icon组件的示例。 You'll see how the props foo and size must be passed when calling the function on line 21.您将看到在第 21 行调用 function 时必须如何传递道具foosize

https://codesandbox.io/s/trusting-banach-prwos?file=/src/App.tsx https://codesandbox.io/s/trusting-banach-prwos?file=/src/App.tsx

https://www.typescriptlang.org/docs/handbook/interfaces.html#function-types https://www.typescriptlang.org/docs/handbook/interfaces.html#function-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿React.FC<Props> 困惑 - TypeScript React.FC<Props> confusion 打字稿 - React.FC 是任何而不是检查道具 - Typescript - React.FC is any and not checking props React.FC 中的泛型类型<Props<T> &gt; - GenericType in React.FC<Props<T>> typescript React.FC如何表达给定道具(来自用户)与组件内的最终道具之间的差异 - typescript React.FC how to express difference between given props(from user) to final props inside the component React.FC 迭代 json 道具 - React.FC iterate over json props React.FC<specificprops> 不可分配给 React.FC<genericprops> ,但道具是可分配的</genericprops></specificprops> - React.FC<SpecificProps> is not assignable to React.FC<GenericProps>, but props are assignable 如何使用 React.FC<props> 当子节点可以是 React 节点或函数时键入 - How to use React.FC<props> type when the children can either be a React node or a function 在 React Typescript 中使用 props 作为 prop 传递组件 - Pass down component with props as prop in React Typescript 当 React 组件在 Typescript 中具有强制道具时,将其作为道具注入 - Inject a React component as a prop when it has mandatory props in Typescript Function 带有功能组件的道具 - 不是 function(React + Typescript) - Function Props with Functional component - is not a function (React + Typescript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM