简体   繁体   English

Typescript JSX 与 Generics - 参数隐式具有“任何”类型

[英]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
});

TS Playground TS游乐场

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.

相关问题 RN打字稿中参数隐式具有任何类型 - Parameter implicitly has any type in RN typescript TypeScript 错误参数“props”隐式具有“any”类型 - TypeScript error Parameter 'props' implicitly has an 'any' type 映射打字稿参数:绑定元素“列”隐式具有“任何”类型 - Map typescript parameter: Binding element 'columns' implicitly has an 'any' type TypeScript:元素隐含地具有 RegExp 的“任何”类型 - TypeScript: Element implicitly has an 'any' type for RegExp TypeScript - 元素隐式具有“任何”类型 [...] 在类型上未找到具有“字符串”类型参数的索引签名 - TypeScript - Element implicitly has an 'any' type [...] No index signature with a parameter of type 'string' was found on type 参数结果隐式具有任何类型 - Parameter result implicitly has any type React typescript 错误:元素隐式具有“任何”类型 - React typescript error: Element implicitly has an 'any' type TypeScript 元素隐含一个“任何” - TypeScript Element implicitly has an 'any' 为什么参数“props”隐式具有“any”类型? - Why does parameter 'props' implicitly has an 'any' type? TS7006:参数“事件”隐式具有“任何”类型 - TS7006: Parameter 'event' implicitly has an 'any' type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM