Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - Import Namespace Into Another Namespace

I'm wondering if it's possible to export a namespace from one typescript .d.ts file and then import that namespace into another .d.ts file where it gets used inside of a namespace.

Example:

namespace_export.d.ts

export namespace Foo {
    interface foo {
        prop1: string;
    }
}

types.d.ts

import { Foo } from './namespace_export'

export namespace Types {
    Foo // <-- This doesn't work but is what I would like
    interface Bar {
        prop2: string
    }
}

testfile.ts

import { Types } from './types'

function testTypes(type: Types.Foo.foo) {
    console.log(type);
}
like image 779
Jon Lamb Avatar asked Jul 29 '17 22:07

Jon Lamb


People also ask

What is a namespace in typescript?

TypeScript - Namespaces. A namespace is a way to logically group related code. This is inbuilt into TypeScript unlike in JavaScript where variables declarations go into a global scope and if multiple JavaScript files are used within same project there will be possibility of overwriting or misconstruing the same variables,...

What is the import users = part in typescript?

The import Users = part is called an “alias” in Typescript and it’s another piece of interesting code. Again took me a loooot of time to bump into it. It’s a variable at runtime but it also has typing information inside. And it can be used as a “wrapper”, the same way namespaces and modules are used.

How do I use namespaces in JavaScript?

Using Namespaces #. Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. They can span multiple files, and can be concatenated using --outFile.

What is the difference between namespaces and modules?

Namespaces are a TypeScript-specific way to organize code. Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. Unlike modules, they can span multiple files, and can be concatenated using outFile .


1 Answers

I was wondering how to achieve this too. I found this solution:

import { Foo as fooAlias } from './namespace_export'
export namespace Types {
  export import Foo = fooAlias;
  interface Bar {
    prop2: string
  }
}

Hope this helps ;)

like image 130
Jorgeblom Avatar answered Oct 22 '22 04:10

Jorgeblom