在 Generic 中检测并使用 Typescript 类型而不重复

问题描述 投票:0回答:0

我正在寻找一种使用元素类型作为默认泛型类型的方法,如果这有意义的话。 我写了一些有用的东西

type asElement<T> = (T & keyof JSX.IntrinsicElements) | undefined;

<Button as={Link as asElement<Link>}>Test</Button>

我想完善类型定义,所以我只需要写

<Button as={Link as asElement}>Test</Button>

应该可以,但是找不到合适的搜索关键词

有人能帮忙吗?

谢谢, 托拜厄斯

我为什么要杜这个??

当使用 react-bootstrap 中的“as”属性时,Typescript 在使用一些非引导组件时会抛出错误,即 Remix 中的 Link 组件

具体错误见下文。我不太明白错误(没有深入调查),但由于我不能/不应该覆盖 react-bootstrap 类型,所以我真的不需要。

功能有效——包括在 IDE 中识别和建议“as”组件的属性。所以,我想,用速记进行类型转换。 但是,我不太清楚我的新类型速记应该是什么样子。 对于那些感兴趣的人,我附上了错误日志。当然,欢迎提供有关替代解决方案的任何提示

TS2322: Type 'ForwardRefExoticComponent<RemixLinkProps & RefAttributes<HTMLAnchorElement>>' is not assignable to type '(ForwardRefExoticComponent<RemixLinkProps & RefAttributes<HTMLAnchorElement>> & keyof IntrinsicElements) | undefined'.
Type 'ForwardRefExoticComponent<RemixLinkProps & RefAttributes<HTMLAnchorElement>>' is not assignable to type 'ForwardRefExoticComponent<RemixLinkProps & RefAttributes<HTMLAnchorElement>> & "symbol"'.
Type 'ForwardRefExoticComponent<RemixLinkProps & RefAttributes<HTMLAnchorElement>>' is not assignable to type '"symbol"'.
javascript typescript generics react-bootstrap typescript-generics
© www.soinside.com 2019 - 2024. All rights reserved.