首页 > 解决方案 > React - 模块解析失败:意外令牌 (5:10)

问题描述

我有一个文件 - deps.ts

export class Deps {  
  private static deps = new Map<any, any>();

  public static get<T>(type: any): T {
    return this.deps.get(type)
      ?? this.add(type, new type());
  }

  public static add<T>(type: any, instance: T): T {       
    return this.deps
      .set(type, instance)
      .get(type);
  }
}

这是其他 TypeScript 项目中的有效源代码,但在 React 中不是。

我收到此错误:

../utils/src/deps.ts 5:10
Module parse failed: Unexpected token (5:10)
File was processed with these loaders:
 * ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
You may need an additional loader to handle the result of these loaders.
| 
| export class Deps {  
>   private static deps = new Map<any, any>();
| 
|   public static get<T>(type: any): T {

当我改变时tsconfig.json,似乎什么都没有发生。它是create-react-app使用 TypeScript 模板制作的,不会被弹出。

版本

源代码https ://github.com/codea-live/dclone

website/src/utils文件夹是指向 的符号链接utils,其中包含deps.ts.

我将如何修复这个错误,或者让 React 忽略utils文件夹中的错误?

标签: javascriptnode.jsreactjstypescripttsconfig

解决方案


推荐阅读