简体   繁体   English

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

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

I have a shared module which is used by an React and Backend module.我有一个由 React 和后端模块使用的共享模块。 It contains two files: index.ts and package.json .它包含两个文件: index.tspackage.json

Package.json Package.json

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

In index.ts there are just exported interfaces like the following one:index.ts中只有导出的接口,如下所示:

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

I have installed it in my React app and can import the interface.我已经将它安装在我的 React 应用程序中,并且可以导入界面。 But when I run the React app it get the following compilation error:但是当我运行 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;

This issue is related to webpack but nowhere in my Git repo is webpack config file.此问题与 webpack 有关,但在我的 Git 存储库中没有 webpack 配置文件。 I use this shared module also in other modules and it works fine.我也在其他模块中使用这个共享模块,它工作正常。

I think that shared module need some more code in order to work with Webpack and React.我认为共享模块需要更多代码才能与 Webpack 和 React 一起使用。 What did I miss?我错过了什么?

UPDATE:更新:

When I change the filename to index.d.ts I get different and probably better error:当我将文件名更改为index.d.ts ,我得到了不同且可能更好的错误:

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

There were the following two mistakes:有以下两个错误:

  1. The filename must be index.d.ts文件名必须是index.d.ts
  2. The Typescript module was not set up correctly. Typescript 模块未正确设置。 It should look like this:它应该如下所示:

index.d.ts索引.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