【问题标题】:How to resolve ' error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.'?如何解决“错误 TS2351:无法将“新”与类型缺少调用或构造签名的表达式一起使用。”?
【发布时间】:2018-03-15 10:55:04
【问题描述】:

我将一个名为 Auth.js 的 .js 文件(不是 .ts 文件)导入到我的 reactjs&typescript 应用程序中,所以在我的组件中我有这个:

import * as Auth from '../Auth/Auth';
..
const auth = new Auth();

这是我的 Auth.js 的一部分:

    export default class Auth {
        auth0 = new auth0.WebAuth({
            domain: AUTH_CONFIG.domain,
            clientID: AUTH_CONFIG.clientId,
            redirectUri: AUTH_CONFIG.callbackUrl,
            audience: `https://${AUTH_CONFIG.domain}/userinfo`,
            responseType: 'token id_token',
            scope: 'openid'
        });

        constructor() {
            this.login = this.login.bind(this);
            this.logout = this.logout.bind(this);
            this.handleAuthentication = this.handleAuthentication.bind(this);
            this.isAuthenticated = this.isAuthenticated.bind(this);
        }
..

当我运行 webpack 时,我收到以下错误消息:

ERROR in ./src/containers/main.tsx
(16,14): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

如何解决此 TS 错误?

【问题讨论】:

标签: javascript reactjs typescript


【解决方案1】:

尝试删除* as。您正在默认导出 Auth。我们直接导入的默认导出属性,不带任何*{}

import Auth from '../Auth/Auth';
..
const auth = new Auth();

【讨论】:

  • new Auth.Auth() 应该也能正常工作,但不确定:),如果您使用 * 导入
  • 当我将标志 'esModuleInterop': true 添加到我的 tsconfig.json 时,这对我来说坏了,建议修复她修复了这个问题。
猜你喜欢
  • 2017-03-25
  • 2021-01-04
  • 2015-09-22
  • 2018-02-06
  • 2018-02-09
  • 1970-01-01
  • 2018-10-17
  • 2017-02-03
  • 2019-09-10
相关资源
最近更新 更多