Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to export * as foo in typescript

I can:

import * as foo from './foo' 

But can't seem to export the same:

export * as foo from './foo' 

This doesn't seem to work either...:

import * as fooImport from './foo'; export const foo = fooImport; 

Any ideas?

--- UPDATE ---

What are you trying to achieve?

Basically, I am working on implementing an ngrx/store backend for my app. I want to organize my code like this:

app/core/   index.ts   viewer/     index.ts     viewer-actions.ts     viewer-reducer.ts   view-data/     index.ts     view-data-actions.ts     view-data-reducer.ts 

And I want to use my index.ts files to chain up all the exports from each subset (common paradigm).

However, I want to keep things namespaced. Each of my xxx-reducer.ts and xxx-actions.ts files have exports of the same name (reducer, ActionTypes, Actions, ...) so normal chaining would result in a name collision. What I am trying to do is allow for all of the exports from xxx-actions and xxx-reducer to be re-exported as xxx. This would allow me to:

import { viewer, viewerData } from './core';  ...     private viewer: Observable<viewer.Viewer>;     private viewData: Observable<viewData.ViewData>;      ngOnInit() {         this.viewer = this.store.let(viewer.getViewer());         this.viewData = this.store.let(viewData.getViewData());     } 

Instead of the more verbose:

import * as viewer from './core/viewer'; import * as viewerData from './core/viewer-data';  ... 

Thats the gist anyway...

like image 604
Lucas Avatar asked Mar 10 '17 21:03

Lucas


People also ask

Can you export a type in TypeScript?

Use a named export to export a type in TypeScript, e.g. export type Person = {} . The exported type can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.

What can you export with module TS exports?

TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

How do I export a class object in TypeScript?

Use named exports to export multiple classes in TypeScript, e.g. export class A {} and export class B {} . The exported classes can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a file.

How do I export a TS file variable?

Use named exports to export multiple variables in TypeScript, e.g. export const A = 'a' and export const B = 'b' . The exported variables can be imported by using a named import as import {A, B} from './another-file' .


1 Answers

TypeScript 3.8 or Later

See https://stackoverflow.com/a/59712387/1108891 for details.

Before TypeScript 3.7 or Earlier

Is it possible to export * as foo in typescript

Nope. You can, however, use a two step process:

src/core/index.ts

import * as Foo from "./foo"; import * as Bar from "./bar";  export {     Foo,     Bar, } 

src/index.ts

import { Foo, Bar } from "./core";  function FooBarBazBop() {     Foo.Baz;     Foo.Bop;     Bar.Baz;     Bar.Bop; } 

src/core/foo/index.ts and src/core/bar/index.ts

export * from "./baz"; export * from "./bop"; 

src/core/foo/baz.ts and src/core/bar/baz.ts

export class Baz {  } 

src/core/foo/bop.ts and src/core/bar/bop.ts

export class Bop {  } 

See also: https://www.typescriptlang.org/docs/handbook/modules.html

like image 189
Shaun Luttin Avatar answered Sep 19 '22 15:09

Shaun Luttin