Reputation: 2298
I'm using Typescript, React & Redux (if relevant). My project structure:
/project
/src
/actions
index.ts
actions.ts
index.ts
import {
Action,
} from "./actions";
export { Action }
I re-export Actions
in index.ts
so that other files (not under the /actions
directory) can import the Actions
type using 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)
I get this error:
Type error: Module '"<snipped path>"' has no exported member 'Action'.
TS2305
When I try to import Action
anywhere.
Any ideas?
Upvotes: 9
Views: 11721
Reputation: 8721
Typescript 3.8 introduces new syntax for type-only imports and exports:
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';
This enables exporting types via index.ts
(module's public API) files as we would do with other exported members. Just add type
keyword in front of destructured types/interfaces:
export type { Actions } from './actions';
In the earlier versions of Typescript (before 3.8) to make it work, it's necessary to reexport imported types:
(looks pretty ugly, but does the job)
import { Actions as _Actions } from "./actions";
export type Actions = Actions;
References:
- https://devblogs.microsoft.com/typescript/announcing-typescript-3-8-beta
- https://github.com/microsoft/TypeScript/pull/35200
Upvotes: 18
Reputation: 18523
Make sure you use named import and not default import when importing actions:
// index.ts
import { Actions } from './actions';
export { Actions }
// elsewhere
import { Actions } from './path/to/src/action';
Or, use default export in both index and elsewhere:
// index.ts
import { Actions } from './actions';
export default Action;
// elsewhere
import Actions from './path/to/src/action';
Upvotes: 0