Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: Export the same function with different names

Tags:

typescript

I have a function that i need to export under two different names without redeclaring it.

Right now i'm stuck at doing this:

function handle() {
    console.log('handle');
}


export function post {
    return handle();
}

export function get() {
    return handle();
}

But it doesn't scale well and it's ugly, especially if i have parameters that i need to pass to handle.

Ideally it would look something like this, but it's not valid syntax:

function handle() {
    console.log('handle');
}

export {handle} as get;

export {handle} as post;

Is there a way to do this?

like image 452
pietrovismara Avatar asked Oct 31 '17 10:10

pietrovismara


2 Answers

I think that you need to change your TypeScript code. You will find more info at the official documentation.

function handle() {
    console.log('handle');
}

export { handle as get };
export { handle as post };

And then you can import it as you want

import { get } from './your-file-path';
like image 119
Javier Fernández Avatar answered Sep 18 '22 14:09

Javier Fernández


Found a solution:

function handle() {
    console.log('handle');
}

export let get = handle;

export let post = handle;
like image 22
pietrovismara Avatar answered Sep 20 '22 14:09

pietrovismara