类型错误:类扩展值未定义不是构造函数或 nulll

时间:2021-02-02 10:45:12

标签: javascript reactjs typescript class

我在运行时遇到上述错误,我将 Create-React-App 与 Typescript 一起使用。

这是我的代码:

src/api/HttpClient.ts

class HttpClient {
  public readonly instance: AxiosInstance;
  public constructor(baseURL: string) {
    this.instance = axios.create({ baseURL });
  }
}
export default HttpClient;

src/api/auth/index.ts

import HttpClient from "../HttpClient";
export class AuthService extends HttpClient {
  public constructor() {
    super("http://dev.api");
  }
  // API Calls
  public MakeHttpRequest = () => this.instance.get("/users");
}

尝试默认导出 AuthService,尝试按名称导出 HttpClient。同样的错误。

只有当这两个类都在同一个文件中时,它才能正常编译和运行,这不是我想要实现的目标。

编辑:

这是我的tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "watch": true,
    "outDir": "./built",
    "allowJs": true,
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "skipLibCheck": true,
    "esModuleInterop": true,
    "noImplicitAny": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": ["./src/**/*"]
}

1 个答案:

答案 0 :(得分:0)

发现我在循环导入文件。已关闭。