【发布时间】:2020-07-22 14:28:11
【问题描述】:
我正在尝试将 https://github.com/catalinmiron/react-typical 移植到 TypeScript。但是,我面临一些问题。
这是 VSCode 中出现错误的屏幕截图:
为简洁起见,下面是相同的代码:
import React from 'react'
import { type, type as loopedType } from '@camwiegert/typical'
import styles from './styles.module.css'
type Props = {
steps: Array<any>
loop: number
className: string
wrapper: React.Component
}
const Typical: React.FC<Props> = ({ steps, loop, className, wrapper = 'p' }) => {
const typicalRef = React.useRef<HTMLElement>(null)
const Component: string = wrapper
const classNames: string[] = [styles.typicalWrapper]
if (className) {
classNames.unshift(className)
}
React.useEffect(() => {
if (loop === Infinity) {
type(typicalRef.current, ...steps, loopedType)
} else if (typeof loop === 'number') {
type(typicalRef.current, ...Array(loop).fill(steps).flat())
} else {
type(typicalRef.current, ...steps)
}
}, [typicalRef])
return <Component ref={typicalRef} className={classNames.join(' ')} />
}
export default React.memo(Typical)
我无法为Component 编写类型。
我也尝试过以下操作:
const Component = React.Component | string
但它在return <Component .../> 附近显示'Component' refers to a value, but is being used as a type here. Did you mean 'typeof Component'?,下划线位于Component 上方。
我也无法将typicalRef 转换为typicalRef.current 总是通过在其下方显示红色波浪线来引发错误。 flat() 和 classNames.join(' ') 也一样。
我正在为此失去理智。似乎无法弄清楚。会喜欢任何指针吗?
【问题讨论】:
-
VSCode 显示的错误是什么?此外,如果您只想忽略它们而不实际修复它们,请在行上方添加
// @ts-ignore(当然,您应该尝试修复它们)。 -
您是否尝试过将变量 Component 重命名为其他名称?
-
@EmreKoc 组件上的第一个错误写在帖子中。阅读
const Component = React.Component | string正下方的行。我很乐意解决它而不是忽略它。 -
@FaisalRashid 没有必要重命名它,因为
Component不是保留关键字,或者我正在从react本身解构Component所以我认为即使我也应该没问题重命名它:)
标签: javascript reactjs typescript react-ref