如何解决'错误TS2351:不能对类型缺少调用或构造签名的表达式使用'new'。'? [英] How to resolve ' error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.'?

查看:1591
本文介绍了如何解决'错误TS2351:不能对类型缺少调用或构造签名的表达式使用'new'。'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

This is part of my 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时,我收到以下错误消息:

When I run webpack I get the following error message:

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错误?

推荐答案

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

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

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

这篇关于如何解决'错误TS2351:不能对类型缺少调用或构造签名的表达式使用'new'。'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆