【发布时间】:2020-09-19 03:41:13
【问题描述】:
我正在使用 TypeScript 在 Gatsby 上创建一个 React 组件,并以这种方式定义了一个画布样式组件:
const Background = styled.canvas`
position: fixed;
width: 100%;
height: 100%;
`;
为了使用它,我以这种方式为其分配类型:
const canvas: HTMLCanvasElement = Background,
context = Background.getContext('2d');
但我在画布类型中遇到此错误:
Type 'String & StyledComponentBase<"canvas", any, {}, never> & NonReactStatics<never, {}>' is missing the following properties from type 'HTMLCanvasElement': height, width, getContext, toBlob, and 238 more.
.getContext() 方法也出现错误:
This expression is not callable.
Type 'never' has no call signatures.
我一直在寻找解决方案,但找不到适合此特定问题的解决方案。 有人可以向我解释一下在 TypeScript 中使用 Styled Component 画布的最佳方式吗?
【问题讨论】:
-
This 可能有点相关。
-
谢谢@AjeetShah,我去看看!
标签: reactjs typescript canvas gatsby styled-components