Typescript 属性在实现接口的类的类型中不存在,编译器错误

Typescript 属性在实现接口的类的类型中不存在,编译器错误,typescript,Typescript,在使用TypeScript的应用程序中,我遇到了这个问题,我不知道如何解决,也不知道为什么会发生 在一个模块中,我有这种类型的代码 // Api.ts interface ApiInterface { signIn(user: object): AxiosPromise authenticated(): AxiosPromise getCurrentUser(): AxiosPromise } export class Api implements ApiInterface {

在使用TypeScript的应用程序中,我遇到了这个问题,我不知道如何解决,也不知道为什么会发生

在一个模块中,我有这种类型的代码

// Api.ts
interface ApiInterface {
  signIn(user: object): AxiosPromise
  authenticated(): AxiosPromise
  getCurrentUser(): AxiosPromise
}

export class Api implements ApiInterface {
  public signIn() {
     ...
  }

  public authenticated() {
     ...
  }

  public getCurrentUser() {
     ...
  }    
}
但问题是,当我在另一个文件中尝试使用
Api
类时,会出现编译错误,如下所示:

import { Api } from './Api'

async function foo() {
  const isAuthenticated = await Api.authenticated() // ERROR
  ...
}
错误表明:类型“typeof Api”上不存在属性“authenticated”。


我该怎么过这关?编译器是否不知道类Api实现了Api接口?

您正在导入类
Api
成员
已验证的
存在于类的实例上。您需要使用
new
操作符创建一个实例

import { Api } from './Api'

async function foo() {
  const api = new Api();
  const isAuthenticated = await api.authenticated() // ERROR
  ...
}

您正在导入类
Api
成员
authenticated
存在于类的实例上。您需要使用
new
操作符创建一个实例

import { Api } from './Api'

async function foo() {
  const api = new Api();
  const isAuthenticated = await api.authenticated() // ERROR
  ...
}