如何使用 Typescript 在函数中包含输出参数? [英] How do I include output parameters in a function with Typescript?

查看:35
本文介绍了如何使用 Typescript 在函数中包含输出参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 TypeScript 在函数中包含输出参数?类似于 C# 中的 Func1(string val1, int out k1, int out k2).

Is it possible to include output parameters in a function with TypeScript? Something like Func1(string val1, int out k1, int out k2) in C#.

推荐答案

目前没有.

您可以返回一个可以包含多个属性的对象.

You can return an object that can contain more than one property.

return { k1: 5, k2: 99 };

您可以将其与解构结合起来,使中间对象变得不可见...

You can combine this with destructuring so the intermediate object becomes invisible...

function myFunction() {
    return { k1: 5, k2: 99 };
}

const { k1, k2 } = myFunction();

console.log(k1);
console.log(k2);

你也可以用元组来实现同样的效果,但它的可读性很好.

You could also achieve the same with a tuple, but this is pretty readable.

这篇关于如何使用 Typescript 在函数中包含输出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆