我正在使用Typescript,React和Redux(如果相关)。我的项目结构:
/project
/src
/actions
index.ts
actions.ts
index.ts
import {
Action,
} from "./actions";
export { Action }
我将Actions
中的index.ts
重新导出,以便其他文件(不在/actions
目录下)可以使用Actions
导入import { Action } from "./actions"
类型>
actions.ts
// These are the general structures create each
// async action
type Request<T> = { request: T }
type Response<T> = { response: T }
type Err = { error: string }
// Alias common types for requests & responses
type EmptyRequest = Request<null>
type ValueRequest = Request<{ value: number }>
export type Action
// UI Actions
= ({ type: "INCREMENT_COUNTER", delta: number })
| ({ type: "RESET_COUNTER" })
// Aync Actions
| ({ type: "SAVE_COUNT_REQUEST" } & ValueRequest)
| ({ type: "SAVE_COUNT_SUCCESS" } & Response<{}>)
| ({ type: "SAVE_COUNT_FAILURE" } & Err)
我收到此错误:
Type error: Module '"<snipped path>"' has no exported member 'Action'.
TS2305
当我尝试导入Action
任何地方时。
有什么想法吗?
答案 0 :(得分:3)
Typescript 3.8引入了仅用于类型导入和导出的新语法:
import type T from './module';
import type { A, B } from './module';
import type * as Types from './module';
export type { T };
export type { T } from './module';
与其他导出成员一样,这可以通过index.ts
(模块的公共API)文件导出类型。 只需在解构的类型/接口前面添加type
关键字:
export type { Actions } from './actions';
在Typescript的早期版本(3.8之前)中,要使其正常工作,必须重新导出导入的类型:
(看起来很丑,但能完成任务)
import { Actions as _Actions } from "./actions";
export type Actions = Actions;
参考文献:
-strict weak ordering on wikipedia
-https://devblogs.microsoft.com/typescript/announcing-typescript-3-8-beta
答案 1 :(得分:0)
确保在导入操作时使用命名导入而不是默认导入:
// index.ts
import { Actions } from './actions';
export { Actions }
// elsewhere
import { Actions } from './path/to/src/action';
或者,在索引和其他位置使用默认导出:
// index.ts
import { Actions } from './actions';
export default Action;
// elsewhere
import Actions from './path/to/src/action';