Skip to content

Exporting imported types as a namespace #20273

Closed

Description

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.

Activity

  1. ORESoftware commented on Nov 26, 2017

    @ORESoftware
    Author

    I found a related issue - #4529

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

  2. changed the title [-]Exporting imported types as a namespace / module[/-] [+]Exporting imported types as a namespace[/+] on Nov 26, 2017
  3. poseidonCore commented on Nov 27, 2017

    @poseidonCore

    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;
    }
    
  4. ORESoftware commented on Nov 27, 2017

    @ORESoftware
    Author

    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.

  5. poseidonCore commented on Nov 28, 2017

    @poseidonCore

    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.

  6. poseidonCore commented on Nov 28, 2017

    @poseidonCore

    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.

  7. typescript-bot commented on Dec 12, 2017

    @typescript-bot
    Contributor

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

  8. ORESoftware commented on Dec 12, 2017

    @ORESoftware
    Author

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

  9. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions