作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码在使用 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.overrides
是ComponentOverride
类型,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/
以下代码在使用 TypeScript 编译时会抛出错误。 import React, { SFC } from "react" import { Link } from "react-router-d
我是一名优秀的程序员,十分优秀!