Skip to content

Exporting imported types as a namespace #20273

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
ORESoftware opened this issue Nov 26, 2017 · 7 comments
Closed

Exporting imported types as a namespace #20273

ORESoftware opened this issue Nov 26, 2017 · 7 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@ORESoftware
Copy link

ORESoftware commented Nov 26, 2017

This question is a bit tricky and I fear I won't get an answer on StackOverflow:

the question is here:
https://stackoverflow.com/questions/47500855/exporting-imports-as-a-namespace-with-typescript

Essentially I want to group together various types from other files into one namespace X. So the types are not declared in namespace X, but they are later grouped into namespace X after the fact. Hopefully that makes sense.

A simple scenario would look like:

import {p} from 'q';
import {foo} from 'bar';
import {zim} from 'zoom';

export namespace X {
    export p;             //  this syntax is not real, don't know what the right 
    export foo;           //  syntax is in actuality
    export zim;
}

we can sort of do this with a const:

export const X = {
  p,
  foo,
  zim
}

but the problem is, for interfaces, I get the error:

only refers to a type, but is being used as a value here.

@ORESoftware
Copy link
Author

I found a related issue - #4529

Not sure if that's the most recent on this.

@ORESoftware ORESoftware changed the title Exporting imported types as a namespace / module Exporting imported types as a namespace Nov 26, 2017
@poseidonCore
Copy link

You can often get around this with notion like this:

import {p as _p} from 'q';
import {foo as _foo} from 'bar';
import {zim as _zim} from 'zoom';

export namespace X {
    export import p = _p;  
    export import foo = _foo;
    export import zim = _zim;
}

or

import * as Q from 'q';
import * as Bar from 'bar';
import * as Zoom from 'zoom';

export namespace X {
    export import p = Q.p;  
    export import foo = Bar.foo;
    export import zim = Zoom.zim;
}

@ORESoftware
Copy link
Author

ORESoftware commented Nov 27, 2017

thanks yeah your solution works in general, but for interfaces, I get that error though "using as value, but only refers to a type", so I have to use the solution/answer that's in the StackOverflow link at the top.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Nov 27, 2017
@poseidonCore
Copy link

The notation that I used works for me with interfaces/types too.

Example

TestInterfaces.ts

export interface I {a:number};
export type T = number|string;

TestLibrary.ts

import * as TestInterfaces from "./TestInterfaces";

export namespace X {
	export import I = TestInterfaces.I;
	export import T = TestInterfaces.T;
}

The problem exists if you try to do an aliased/selective import. The following will generate the error that you cite:

TestLibrary.ts

import {I as I2} from "./TestInterfaces";
import {T as T2} from "./TestInterfaces";

export namespace X2 {
	export import I = I2;
	export import T = T2;
}

An there is no pretty way to bundle these aliased interfaces/types unless you do something cumbersome like this:

TestLibrary.ts

import {I as I2} from "./TestInterfaces";
import {T as T2} from "./TestInterfaces";

export namespace X3 {
	export interface I extends I2 {};
	export type IAsType = I2;
	export type T = T2;
}

I agree that just having a convenient re-export notation like you have stated would be optimum.

@poseidonCore
Copy link

I also meant to say, if you want to keep the convenience of export const for re-exported objects/values, you can still conflate the namespace X after it with re-exported types/interfaces:

export const X = {
  p,
  foo,
  zim
}

export namespace X {
    export import someType = Q.someType;  
    export import SomeInterface = Bar.SomeInterface;
}

But it's still not pretty.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@ORESoftware
Copy link
Author

I solved this problem with the accepted answer to this question:
https://stackoverflow.com/questions/47500855/exporting-imports-as-a-namespace-with-typescript

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants