Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: import and export "at the same time"

Is there a way of doing an import and an export of the same named variable, at the same time, in the same TypeScript file?

For instance, the following situation:

import { State as UIState } from './ui'
import { State as ConnectionState } from './connection'

export { State as UIState } from './ui'
export { State as ConnectionState } from './connection'

export type State = {
    ui: UIState
    connection: ConnectionState
}

To reduce writing cost, I would like to be able to write something like that:

export import { State as UIState } from './ui'

Any ideas?

like image 375
Samuel Avatar asked Feb 01 '18 03:02

Samuel


People also ask

How do I import and export interface in TypeScript?

Use a named export to export an interface in TypeScript, e.g. export interface Person{} . The exported interface 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.

Can you use module exports with import?

The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the import declaration or dynamic import.

What is the module and how it can be imported and exported in TypeScript with example?

We can create a module by using the export keyword and can use in other modules by using the import keyword. Modules import another module by using a module loader. At runtime, the module loader is responsible for locating and executing all dependencies of a module before executing it.

How do I import export classes in TypeScript?

The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum. When exporting a module using export = , TypeScript-specific import module = require("module") must be used to import the module.


2 Answers

Is there a way of doing an import and an export of a the same named variable, at the same time, in the same TypeScript file

No. There is no JS module syntax for that.

like image 70
basarat Avatar answered Oct 17 '22 13:10

basarat


Creating a "index.ts" file (or foo.ts) reduced the repetitive import/export. (I am not sure if this is a good solution)

export * from './product-form.component'
export * from './product-list.component'

Angular module

import { NgModule } from '@angular/core';
import * as c from './index';
export * from './index'

@NgModule({
    declarations: [ 
        c.ProductListComponent,
        c.ProductFormComponent, 
    ]
  })
  export class ProductModule { }
like image 22
Davi Fiamenghi Avatar answered Oct 17 '22 14:10

Davi Fiamenghi