30

If I want to export all the exported members of a module (including code), I can use

export * from 'module'

If I want to export the type of a class without code I can do

export type {typeName} from 'module'

Now I have a need to export all the types from a module, without code. I would be tempted to do

export type * from 'module'

as that's the intuitive thing, but typescript type exports must be named exports (ts 1383).

So how I can I export everything from a module in such a way that its members cannot be used at runtime?

Workarounds I can think of:

  1. Get over it and use export *. This is an option. But for our use case I don't want users of our library to use these classes, only to use them to annotate their types

  2. Ask pretty-please that everyone who uses our library please use import type {typeName} from 'myLibrary'. I guess this is an option too, but how do I convince users to do this?

tl;dr: How can I emulate global type exports in Typescript?

4
  • I'm not sure I understand your scenario, aren't you supposed to export only public code and types to the user so he can only use those explicitly exported? Or you just have too many types and want some kind of "shortcut" to export? Commented Jan 6, 2021 at 16:21
  • The latter. To be very specific, I was working on this PR and I wanted to export all the types from another module from this module. So your point about only exporting public code is valid. In this case, all the code is public, but it's public in another library. Commented Jan 8, 2021 at 22:15
  • 1
    @zhuber Essentially the library I just linked is a wrapper and helper library for another library. In order for this library to function successfully as a "wrapper", users of the wrapper will need the type info from the underlying library. So instead of listing every type individually, I would have liked to just done an export type {*} from 'accesscontrol' Commented Jan 8, 2021 at 22:17
  • have something like types.ts inside a folder or file containing all global types.. and then in your index.ts just do export * from './types' something like this we have done here Commented Jan 11, 2021 at 11:45

2 Answers 2

14

As stated in your referenced pull request, wildcard reexporting of types is not yet implemented (and may never). Hence reexporting all types to the root level is currently not possible. The closest you can get is by wrapping the reexported types in their own named collection:

// your-module
import type * as Types from "external-module";
export { Types };

// Usage by the end user
import { Types } from "your-module";
let a:Types.Class;

The drawback of this approach is that the user is not able to import the types independently like import { Class } from "your-module";. The types are exported as type only though, so this would still have no effect on the size of your bundle and one still can't instantiate the exported types.

Sign up to request clarification or add additional context in comments.

6 Comments

what if end users want to access Types.Class as Class without name Type?
I am able to do export type * from 'package'
Can you tell us more @busterroni ?
Doing export type * from 'package' enabled me to do what I was trying to do, related to the question. Sorry that's not more helpful.
@busterroni That works for you? When I do that I get Only named exports may use 'export type'. ts(1383)
|
0

For those coming via Google, I ended up adding a separate entry point. My build process passes all exported types to the dist/types.js (from src/types.ts)

Adding the below to package.json, I was able to import without having to export every type at the top-level.

"exports": {
    ".": {
        "import": "./dist/index.js",
        "require": "./dist/index.js"
    },
    "./types": {
        "import": "./dist/types.js",
        "require": "./dist/types.js"
    }
},

Then to import, you just extend the path for the module (note the /types):

import { MyType } from "module_name/types";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.