gpt4 book ai didi

typescript :导出具有不同名称的相同功能

转载 作者:搜寻专家 更新时间:2023-10-30 20:42:04 26 4
gpt4 key购买 nike

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

现在我坚持这样做:

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


export function post {
return handle();
}

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

但它不能很好地扩展并且很难看,尤其是当我有需要传递给 handle 的参数时。

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

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

export {handle} as get;

export {handle} as post;

有办法吗?

最佳答案

我认为您需要更改 TypeScript 代码。您可以在 official documentation 找到更多信息。 .

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

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

然后就可以随意导入了

import { get } from './your-file-path';

关于 typescript :导出具有不同名称的相同功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47033717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com