使用接口而不导入它们

I'm currently working on a new project that has a client and a server folder, where the names speak for them.

客户端是一个React应用,而服务器是由Express提供支持的Google Cloud功能。

I have a lot of types that are shared (e.g. the User interface), but currently, I have a types folder with user.ts in both of my application folders. Of course, this repetition is far from ideal.

To give you a general idea, this is what the user.ts file looks like

export default interface IUser {
  id: string;
  email: string;
  uid: string;
  username: string;
  balance: number;
}

Is there any way I can share these types throughout my application? Maybe even without importing them? I've read about using a d.ts file, but that seems like a dirty hack?

评论
放弃幸福
放弃幸福

You can rename user.ts file to user.d.ts and then declare your interfaces in order to avoid importing and declare and use in whole application codes.

declare interface IUser {
  id: string;
  email: string;
  uid: string;
  username: string;
  balance: number;
}
点赞
评论