开始制作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中导出状态接口的定义。
当我简单地将State
放在导出中时,它告诉我'State' only refers to a type, but is being used as a value here.
,我知道这是错误的方式,但是导出此定义需要什么?
答案 0 :(得分:2)
问题是您试图导出对象,但Typescript类型和接口仅在编译之前存在。该键的对象值是什么?
// Typescript
interface Foo {};
export default {
Foo,
};
// Compiled Javascript
export default {
Foo: ???
};
我确实同意TS支持这种格式是合乎逻辑的,因为您可以单独导出接口,但是据我所知,您目前无法执行此操作,需要单独进行导出。< / p>
export interface Foo {};
export default {
// Rest of the things
};