繁体   English   中英

为什么 React 不使用接口编译我的本地模块?

[英]Why does React not compile my local module with an Interface?

我有一个由 React 和后端模块使用的共享模块。 它包含两个文件: index.tspackage.json

Package.json

{
  "name": "app-shared",
  "version": "1.0.0",
  "license": "ISC"
}

index.ts中只有导出的接口,如下所示:

export interface Student extends StudentDto {
    ref: any;
    id: string;
    areAllPaid: boolean;
}

我已经将它安装在我的 React 应用程序中,并且可以导入界面。 但是当我运行 React 应用程序时,会出现以下编译错误:

Failed to compile.

../shared/index.ts 4:7
Module parse failed: Unexpected token (4:7)
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.
| $RefreshSetup$(module.id);
| 
> export interface Student extends StudentDto {
|     ref: any;
|     id: string;

此问题与 webpack 有关,但在我的 Git 存储库中没有 webpack 配置文件。 我也在其他模块中使用这个共享模块,它工作正常。

我认为共享模块需要更多代码才能与 Webpack 和 React 一起使用。 我错过了什么?

更新:

当我将文件名更改为index.d.ts ,我得到了不同且可能更好的错误:

Module not found: Can't resolve 'shared' in 'path/path'

有以下两个错误:

  1. 文件名必须是index.d.ts
  2. Typescript 模块未正确设置。 它应该如下所示:

索引.d.ts

declare namespace share {
    export interface Student extends StudentDto {
        ref: any;
        id: string;
        areAllPaid: boolean;
    }
}

export = share;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM