无法将名称空间“ RouteComponentProps”用作类型

时间:2019-02-13 08:30:52

标签: reactjs typescript

当我使用打字稿时,定义一个扩展RouteComponentProp的接口

我遇到了一些问题:

vscode错误:[ts]不能将命名空间“ RouteComponentProps”采用类型

控制台错误:不能使用名称空间“ RouteComponentProps”作为类型。

  • index.tsx
import React from 'react';
import  Styles from './header.less';
import { Link, withRouter, RouteComponentProps } from 'react-router-dom'



interface Props extends RouteComponentProps<any>{} //it can not be work well


class Header extends React.Component<Props, States> {.....}

  • tsconfig.json

  "compilerOptions": {
      "outDir": "./dist/",
      "sourceMap": true,
      "jsx": "react",
      "esModuleInterop": true,
      "allowSyntheticDefaultImports":true,
      "lib": ["es6", "dom"],
      "strictNullChecks": true,
      "target": "es2016",
      "module": "ESNext",  // !important!
      "moduleResolution": "node",
      "strict": true,
      "typeRoots": [
        "node_modules/@types"
    ],
  },
  "include": [
      "./src/**/*",
      "./externals.d.ts"
  ],
  "exclude": [ "node_modules" ]
}
  • externals.d.ts
declare module '*.less'
declare module 'react-router-dom'

有人可以提出建议吗?谢谢

1 个答案:

答案 0 :(得分:1)

使用react-router代替react-router-dom

 import { RouteComponentProps } from 'react-router'