gpt4 book ai didi

reactjs - 为什么类型 SFC 不能分配给类型 SFC<{}>?

转载 作者:行者123 更新时间:2023-12-05 05:05:44 59 4
gpt4 key购买 nike

以下代码在使用 TypeScript 编译时会抛出错误。

import React, { SFC } from "react"
import { Link } from "react-router-dom"
import { HashLink } from "react-router-hash-link"
import MarkdownToJSX from "markdown-to-jsx"

interface AnchorProps {
baseUrl: string
relativeUrl: string
href: string
}

const Anchor: SFC<AnchorProps> = function(props) {
/*
FYI, props.baseUrl and props.relativeUrl is used by code I didn’t include in
this example to keep things simple.
*/
if (
props.href.match(/^http(s)?:\/\//) ||
props.href.match(/^mailto:/) ||
props.href.match(/^tel:/)
) {
return (
<a href={props.href} rel="noopener noreferrer" target="_blank">
{props.children}
</a>
)
} else if (props.href.match(/^#/)) {
return (
<HashLink to={props.href} smooth>
{props.children}
</HashLink>
)
} else {
return <Link to={props.href}>{props.children}</Link>
}
}

const Markdown = function() {
return (
<MarkdownToJSX
options={{
overrides: {
a: {
component: Anchor,
props: {
baseUrl: "/privacy-guides",
relativeUrl: "",
},
},
},
}}
>
# This is [markdown](markdown)
</MarkdownToJSX>
)
}

export default Markdown
/Users/sunknudsen/Sites/sunknudsen/sunknudsen-website/src/Test.tsx
TypeScript error in /Users/sunknudsen/Sites/sunknudsen/sunknudsen-website/src/Test.tsx(40,13):
No overload matches this call.
Overload 1 of 2, '(props: Readonly<MarkdownProps>): Markdown', gave the following error.
Type 'FunctionComponent<AnchorProps>' is not assignable to type 'string | SFC<{}> | ComponentClass<{}, any>'.
Type 'FunctionComponent<AnchorProps>' is not assignable to type 'SFC<{}>'.
Types of parameters 'props' and 'props' are incompatible.
Type '{ children?: ReactNode; }' is not assignable to type 'PropsWithChildren<AnchorProps>'.
Type '{ children?: ReactNode; }' is missing the following properties from type 'AnchorProps': baseUrl, relativeUrl, href
Overload 2 of 2, '(props: MarkdownProps, context?: any): Markdown', gave the following error.
Type 'FunctionComponent<AnchorProps>' is not assignable to type 'string | SFC<{}> | ComponentClass<{}, any>'.
Type 'FunctionComponent<AnchorProps>' is not assignable to type 'SFC<{}>'. TS2769

38 | overrides: {
39 | a: {
> 40 | component: Anchor,
| ^
41 | props: {
42 | baseUrl: "/privacy-guides",
43 | relativeUrl: "",

我不明白为什么。

最佳答案

这似乎是 markdown-to-jsx 类型定义的限制。

具体来说,MarkdownToJSX.options.overridesComponentOverride类型,ComponentOverride是这个类型:

export type ComponentOverride = string | React.ComponentClass | React.SFC | {
component: string | React.ComponentClass | React.SFC;
props?: any;
};

该类型不允许您将泛型参数传递给 React.SFC,这意味着它采用其默认泛型参数:

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

该默认通用参数是一个空对象 {}

由于您的代码试图将 AnchorProps 类型分配给空对象类型,因此编译器会报错。

这是否会成为运行时的问题取决于底层 JavaScript 在运行时的工作方式。像这样转换可能没问题:

const Markdown = function() {
return (
<MarkdownToJSX
options={{
overrides: {
a: {
component: Anchor,
props: {
baseUrl: "/privacy-guides",
relativeUrl: ""
}
// We have tested that this works at runtime,
// we we know more than the compiler knows.
} as ComponentOverride
}
}}
>
# This is [markdown](markdown)
</MarkdownToJSX>
);
};

关于reactjs - 为什么类型 SFC<AnchorProps> 不能分配给类型 SFC<{}>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60382399/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com