Issue
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:
SomeFunction(name1: boolean, name2: boolean, name3: boolean, name4: boolean) //will occur only 1 time, so the change should be in typescript
JavaScript:
$(function () {
...SomeFunction({name1:false, name2:false, name3:false, name4:true}); //will occur 100 times
});
I was looking at(this did not work out):
Named parameters in javascript
How can I add optional named parameters to a TypeScript function parameter?
What can I do in TypeScript, to use named parameters in JavaScript?
What I wonder is, that VS2015 did not show a syntax error when using named parameter the way I used it in TypeScript ...
ps.: I use TS 2.1
Solution
True named parameters don't exist in JavaScript nor in TypeScript but you can use destructuring to simulate named parameters:
interface Names {
name1: boolean
name2: boolean
name3: boolean
name4: boolean
}
function myFunction({name1, name2, name3, name4}: Names) {
// name1, etc. are boolean
}
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
}
Answered By - Paleo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.