简体   繁体   English

Redux商店状态导出接口

[英]Exporting interface of redux store state

Having started making a redux module, I have created the following files: 开始制作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?
};

but I'm not sure how to export the definition of state interface in the index.tsx. 但是我不确定如何在index.tsx中导出状态接口的定义。

When I simply put State in the export, it tells me 'State' only refers to a type, but is being used as a value here. 当我简单地将State放在导出中时,它告诉我'State' only refers to a type, but is being used as a value here. and I understand it's wrong way, but what is needed to export this definition? 我知道这是错误的方法,但是导出此定义需要什么?

The problem is that you're trying to export an object but Typescript types and interfaces only exist before compilation. 问题是您正在尝试导出对象,但是Typescript类型和接口仅在编译之前存在。 What would the object value be for that key? 该键的对象值是什么?

// Typescript
interface Foo {};

export default {
  Foo,
};

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

I do agree that it would be logical for TS to support this formation as you can just export the interface separately, but as far as I know, you can't do this at the moment, you need a separate export. 我确实同意TS支持这种形式是合乎逻辑的,因为您可以单独导出接口,但是据我所知,您目前无法做到这一点,需要单独的导出。

export interface Foo {};

export default {
  // Rest of the things
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM