javascript - redux store state 的导出接口(interface)

标签 javascript typescript interface react-redux

开始制作 redux 模块后,我创建了以下文件:

//state.tsx
export default interface State {
  readonly user: any;
  readonly isLoggedIn: boolean;
}


//types.tsx
export default {
  REQUEST: 'authentication/REQUEST',
  SUCCESS: 'authentication/SUCCESS',
  FAILURE: 'authentication/FAILURE',
  LOGOUT: 'authentication/LOGOUT'
};


//reducers.tsx
import Types from './types';
import State from './state';
import { Reducer, AnyAction } from 'redux';

const initialState: State = {
  user: null,
  isLoggedIn: false
};

export default class {
  reducer: Reducer<State> = (
    state: State = initialState,
    action: AnyAction
  ) => {
    // brahbrah
  };
}

//index.tsx
import reducer from './reducers';
import Types from './types';
import State from './state';

export default {
  reducer,
  Types,
  // How to export State in this default export?
};

但我不确定如何在 index.tsx 中导出状态接口(interface)的定义。

当我简单地将 State 放在导出中时,它告诉我 'State' 仅指一种类型,但在这里用作值。 我明白了这是错误的方式,但是导出这个定义需要什么?

最佳答案

问题是您正在尝试导出对象,但 Typescript 类型和接口(interface)仅在编译之前存在。该键的对象值是什么?

// Typescript
interface Foo {};

export default {
  Foo,
};

// Compiled Javascript
export default {
  Foo: ???
};

我同意 TS 支持这种形式是合乎逻辑的,因为你可以单独导出接口(interface),但据我所知,你目前不能这样做,你需要单独导出。

export interface Foo {};

export default {
  // Rest of the things
};

关于javascript - redux store state 的导出接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554491/

相关文章:

javascript - 如何在 CSS 中向下移动剪贴蒙版?

javascript - jquery background-position-x 问题

javascript - 检测解除设备限制

javascript - 获取特定时间段在另一个时间段的小时数

html - 无法使用 Angular 6 Type 脚本在打印预览页面上使用 CSS 呈现数据

java - 如何避免 'Interface Abstract class error' ?

c# - 使用另一个接口(interface)属性的 IList 实现接口(interface)的类...如何?

javascript - 通过 JavaScript 函数将文本写入 Canvas

typescript - 如何从 Angular 6 中的 blob URL 下载音频文件?

java - 接口(interface) java/Android 不能从子 fragment 到父 fragment