How to fix “TS2349: Cannot invoke an expression whose type lacks a call signature”

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

The DefinitelyTyped library:

declare module "history/lib/createBrowserHistory"  {     export default function createBrowserHistory(options?: HistoryModule.HistoryOptions): HistoryModule.History } 

gives the compile error in the title when used like this (although it worked in plain old .jsx before converting to .tsx):

import React = require('react'); import reactDom = require('react-dom'); import ReactRouter = require('react-router'); import createBrowserHistory = require('history/lib/createBrowserHistory'); import routes = require('app/tools/routes');  export function createReactApp() : void {     let history = createBrowserHistory(); // <-- error :(     reactDom.render     (         <ReactRouter.Router history={history}>{routes}</ReactRouter.Router>,         document.getElementById('app')     ); } 

What am I doing wrong?

回答1:

The module you are trying to use is an ES6 module, so you must import it using ES6 syntax:

import createBrowserHistory from 'history/lib/createBrowserHistory'; 

Alternatively you can use the legacy import = syntax and access the default property of the exported object instead for the default export (but you shouldn’t; use the ES6 import!).



回答2:

If you are having issues, it's likely due to that version being compiled by Babel which is incompatible with TypeScript imports.

Instead, try importing it like this:

import { createBrowserHistory } from 'history'; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!