pietrovismara
pietrovismara

Reputation: 6291

Typescript: Export the same function with different names

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?

Upvotes: 14

Views: 10142

Answers (2)

Javier Fernández
Javier Fernández

Reputation: 316

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';

Upvotes: 29

pietrovismara
pietrovismara

Reputation: 6291

Found a solution:

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

export let get = handle;

export let post = handle;

Upvotes: 4

Related Questions