从单个文件导出typescript类和接口

时间:2021-10-04 07:10:38

I want to be able to export some of my classes/interfaces/enums from other files in a single file. This is how I have done it in javascript:

我希望能够从一个文件中的其他文件中导出我的一些类/接口/枚举。这是我在javascript中完成的方式:

module.exports = {
    Something = require("./src/something").default,
    SomethingElse = require("./src/something-else").default
}

I've noticed that I do not get intellisense from my webstorm (jetbrains) editor, and I'm pretty sure that there is an easier way to implement this in typescript. I've heard and read about modules but I still don't get what they purpose is, most likely they could help me here.

我注意到我没有从我的webstorm(jetbrains)编辑器获得intellisense,我很确定在打字稿中有一种更简单的方法来实现它。我听说过有关模块的内容,但我仍然没有得到它们的用途,很可能它们可以帮助我。

I want to be able to use this style on the library consumer:

我希望能够在库使用者身上使用这种风格:

import { Something, SomethingElse } from "my-ts-library";
...

Any ideas?

有任何想法吗?

1 个解决方案

#1


2  

You can flow my question:

你可以提出我的问题:

Something.ts

Something.ts

export default (a: number, b: number): number => {
  return a + b;
};

SomethingElse.ts

SomethingElse.ts

export default (a: number, b: number): number => {
  return a - b;
};

my-ts-library.ts

我-TS-​​library.ts

import add from './src/Something.ts';
import sub from './src/SomethingElse.ts';
export {
  add,
  sub,
};

using your lib

使用你的lib

import { add, sub } from "my-ts-library";
...

#1


2  

You can flow my question:

你可以提出我的问题:

Something.ts

Something.ts

export default (a: number, b: number): number => {
  return a + b;
};

SomethingElse.ts

SomethingElse.ts

export default (a: number, b: number): number => {
  return a - b;
};

my-ts-library.ts

我-TS-​​library.ts

import add from './src/Something.ts';
import sub from './src/SomethingElse.ts';
export {
  add,
  sub,
};

using your lib

使用你的lib

import { add, sub } from "my-ts-library";
...

相关文章