[英]Typescript JSX with Generics - Parameter implicitly has an 'any' type
When using JSX syntax with Generics, Typescript is able to infer properties normally, except the type of function parameters.当使用带有 Generics 的 JSX 语法时,Typescript 能够正常推断属性,除了 function 参数的类型。
Example code:示例代码:
interface Dictionary {
a: JSX.IntrinsicElements['a'];
button: JSX.IntrinsicElements['button'];
}
type Props<T extends 'a' | 'button'> = Dictionary[T] & {
as: T;
};
function Test<T extends 'a' | 'button'>(args: Props<T>) {
return null;
}
<Test as="a" href="#" onClick={(arg) => {}} />; // Parameter 'arg' implicitly has an 'any' type.
Test({
as: 'a',
href: '#',
onClick: (arg) => {}, // No error
});
If I place the mouse over the onClick property, it can tell the type of the onClick (React.MouseEventHandler<HTMLAnchorElement>), but still cannot infer the parameter's type.如果我将鼠标放在 onClick 属性上,它可以判断 onClick 的类型(React.MouseEventHandler<HTMLAnchorElement>),但仍然无法推断参数的类型。
Just explicitly define it as any with args: any
and it should stop showing it as error:)只需使用args: any
明确将其定义为 any ,它应该停止将其显示为错误:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.