开始制作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.
并且我理解这是错误的方式,但是导出这个定义需要什么?
问题是您正在尝试导出对象,但只有在编译之前存在Typescript类型和接口。对象的价值是什么?
// Typescript
interface Foo {};
export default {
Foo,
};
// Compiled Javascript
export default {
Foo: ???
};
我同意TS支持这种形式是合乎逻辑的,因为你可以单独导出接口,但据我所知,你现在不能这样做,你需要一个单独的导出。
export interface Foo {};
export default {
// Rest of the things
};