Javascript 如何解决“错误 TS2351:无法将 'new' 用于类型缺少调用或构造签名的表达式。”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46558215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:29:59  来源:igfitidea点击:

How to resolve ' error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.'?

javascriptreactjstypescript

提问by bier hier

I am importing a .js file (so not a .ts-file) called Auth.js into my reactjs&typescript application, so in my component I have this:

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

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

This is part of my Auth.js:

这是我的 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);
        }
..

When I run webpack I get the following error message:

当我运行 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.

How can I resolve this TS-error?

如何解决此 TS 错误?

回答by Kishan Mundha

Try by removing * as. You are exporting Authas default. Default export property we import directly without any *or {}.

尝试删除* as. 您正在导出Auth为默认值。我们直接导入的默认导出属性没有任何*{}

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