使用命名参数JavaScript(基于打字稿) [英] Using named parameters JavaScript (based on typescript)
问题描述
我有问题,在TypeScript中使用命名参数时,我知道它不支持我在TS中使用它的方式。
但我怎么能
I have problems, when using named parameter in TypeScript, I know it is not supportetd the way I use it in TS. But how can I
TypeScript:
TypeScript:
SomeFunction(name1: boolean, name2: boolean, name3: boolean, name4: boolean) //will occur only 1 time, so the change should be in typescript
JavaScript:
JavaScript:
$(function () {
...SomeFunction({name1:false, name2:false, name3:false, name4:true}); //will occur 100 times
});
我在看(这没有用):
我该怎么办?将可选的命名参数添加到TypeScript函数参数?
我可以在TypeScript中做什么来在JavaScript中使用命名参数?
What can I do in TypeScript, to use named parameters in JavaScript?
我想知道的是,VS2015在使用命名参数时没有出现语法错误,就像我在TypeScript中使用它一样......
What I wonder is, that VS2015 did not show a syntax error when using named parameter the way I used it in TypeScript ...
ps。:我使用TS 2.1
ps.: I use TS 2.1
推荐答案
您可以使用命名参数:
interface Names {
name1: boolean
name2: boolean
name3: boolean
name4: boolean
}
function myFunction({name1, name2, name3, name4}: Names) {
// name1, etc. are boolean
}
通知:类型名称
实际上是可选的。以下JavaScript代码(无需输入)在TS中有效:
Notice: The type Names
is actually optional. The following JavaScript code (without typing) is valid in TS:
function myFunction({name1, name2, name3, name4}) {
// name1, etc. are of type any
}
A精度:实际上,此功能 destructuring 用于模拟命名参数。在JavaScript和TypeScript中都不存在真正的命名参数。
A precision: in reality, this feature is destructuring used to simulate named parameters. True named parameters don't exist in JavaScript nor in TypeScript.
这篇关于使用命名参数JavaScript(基于打字稿)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!