打字稿:用不同的名称导出相同的函数

pie*_*ara 6 typescript

我有一个函数,需要以两个不同的名称导出而无需重新声明。

现在,我坚持这样做:

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


export function post {
    return handle();
}

export function get() {
    return handle();
}
Run Code Online (Sandbox Code Playgroud)

但这伸缩性不好,而且很难看,尤其是当我有需要传递给的参数时handle

理想情况下,它看起来像这样,但是它是无效的语法:

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

export {handle} as get;

export {handle} as post;
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

Jav*_*dez 6

我认为您需要更改TypeScript代码。您可以在官方文档中找到更多信息。

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

export { handle as get };
export { handle as post };
Run Code Online (Sandbox Code Playgroud)

然后您可以根据需要导入它

import { get } from './your-file-path';
Run Code Online (Sandbox Code Playgroud)

  • 不,我不会说英语:)我会改变它。对不起 (2认同)