javascript - Hur löser man 'fel TS2351: Kan inte använda 'new' med ett uttryck vars typ saknar en anrops- eller konstruktionssignatur.'?


Jag importerar en .js-fil (så inte en .ts-fil) som heter Auth.js till min reactjs&typescript-applikation, så i min komponent har jag detta:

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

Detta är en del av min 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);
        }
..

När jag kör webpack får jag följande felmeddelande:

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

Hur kan jag lösa detta TS-fel?

Svar #1

Försök genom att ta bort * as. Du exporterar Auth som standard. Standardexportegenskap importerar vi direkt utan någon * eller {}.

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

Relaterade artiklar