Skip to content

Export a TypeScript class from another file than it has been defined in #10058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
loilo opened this issue Jul 31, 2016 · 5 comments
Closed

Export a TypeScript class from another file than it has been defined in #10058

loilo opened this issue Jul 31, 2016 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@loilo
Copy link

loilo commented Jul 31, 2016

This is a question I first asked on StackOverflow but apparently nobody actually knows. I'm posting it here because it looks like a quite common scenario which I think is either already achievable somehow (which then should be in the docs in my opinion) or impossible on purpose for which the design-wise reasons would be interesting.

I wrote a small library in TypeScript and I've been trying to export its classes in a single place. The goal here is to make the classes available to a browser via a (nested) global object represented by nested TypeScript namespaces. My goal on the other hand is for each class to be agnostic of how they are made available to the outside.

Let's assume I've got a class Foo

// lib/foo.ts
export class Foo {}

Now I want to make this available to whoever uses the package via the MyModule namespace defined in another file. How would I achieve this? I thought of two potential solutions to this, but both do have their downsides which makes them basically useless.

I could define a variable and assign the class to it:

// api.ts
import { Foo as MyFoo } from "./lib/foo";

export namespace MyModule {
    export const Foo = MyFoo;
}

This will in fact allow me to create instances of the class from an external place like new MyModule.Foo but won't give me the possibility to use them as types since then

import { MyModule } from "path/to/module/api";

let myFooInstance: MyModule.Foo;

will throw a TypeScript error stating that

Module "path/to/module/api" has no exported member 'Foo'

The other way around I tried to use export type Foo = MyFoo instead of const and while this is usable as a type I obviously can't instantiate the class since it's just an alias.

So: Is it generally possible to export classes from namespace in a third place, did I overlook a certain practice for this or is this not possible by design?

@RyanCavanaugh
Copy link
Member

The other way around I tried to use export type Foo = MyFoo instead of const and while this is usable as a type I obviously can't instantiate the class since it's just an alias.

You can do both:

export const Foo = MyFoo;
export type Foo = MyFoo;

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 31, 2016
@loilo
Copy link
Author

loilo commented Aug 1, 2016

...and now I feel a bit dumb. Thank you, would've never thought how easy it could be.

@SOF3
Copy link

SOF3 commented Feb 4, 2018

Is it possible to do this with a namespace too?

@mhegazy
Copy link
Contributor

mhegazy commented Feb 5, 2018

Is it possible to do this with a namespace too?

export import N = M;

@GalHalupSeaLights
Copy link

GalHalupSeaLights commented Jun 12, 2018

Is it possible to do it on a Generic Interface? I want to be able to export IMyInterface so users of the library will be able to create:
let x = new MyClass()

Where MyClass implements IMyInterface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants