如何在打字稿中导出接口集合

Ste*_*mer 5 interface typescript

我有一个actions.ts声明了一堆操作类型的文件。

动作.ts

export interface Action1 extends Action {
type: types.Action1Type
}

export interface Action2 extends Action {
type: types.Action2Type
}

export interface Action3 extends Action {
type: types.Action3Type
}
Run Code Online (Sandbox Code Playgroud)

我使用以下命令将这些操作导入到 index.ts 中:

import * as actions from './actions';
Run Code Online (Sandbox Code Playgroud)

actionsindex.ts 文件中变量的类型是什么?我希望能够在 actions.ts 文件本身中创建此构造并将其导出。类似于以下内容:

动作.ts

interface Action1 extends Action {
type: types.Action1Type
}

interface Action2 extends Action {
type: types.Action2Type
}

interface Action3 extends Action {
type: types.Action3Type
}

export const Actions = {
  Action1,
  Action2,
  Action3,
}
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误:

Action1仅指类型,但在这里用作值。

sro*_*oes 5

接口只是 TypeScript 中的类型定义,因此您不能使用操作创建变量(将它们用作值)。

您可以使用命名空间来组合您的操作:

namespace Actions {

  export interface Action1 extends Action {
    type: types.Action1Type
  }

  export interface Action2 extends Action {
    type: types.Action2Type
  }

  export interface Action3 extends Action {
    type: types.Action3Type
  }

}
Run Code Online (Sandbox Code Playgroud)

这允许您执行以下操作:

import {Actions} from './actions';`
Run Code Online (Sandbox Code Playgroud)

但这基本上是一样的import * as actions from './actions';。您不能将Actions其用作值。