TypeScript报错与解决方案

1,828 阅读1分钟
  • 错误 TS2339: Property '*' does not exist on type 'Node & ParentNode'.

图片.png

解决方法:这是节点未声明类型导致,将节点声明为HTMLElement或HTMLDivElement等元素类型,如下:

(el.parentNode as HTMLElement).scrollTop

  • 错误 TS2339: Property 'webkitHidden' does not exist on type 'Document'.

图片.png

解决方法:在Ts全局声明文件d.ts声明一下Document即可

declare interface Document {
    webkitHidden:any;
}

  • 错误 TS2349: This expression is not callable. Type '*' has no call signatures.

图片.png 旧代码中使用了 import * as 报错,如 import * as Loadable from 'react-loadable'

解决方法:将 * as 去掉,改成 import Loadable from 'react-loadable'


  • 错误 TS2339。类型props上不存在属性“location”。

图片.png

解决方法:引入RouteComponentProps类型

    import { RouteComponentProps } from 'react-router-dom'
    
    export const Test:React.FC<RouteComponentProps> = (props) => {
        const { search } = props.location
        ...
    }