Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  115] [ 1]  / answers: 1 / hits: 59406  / 7 Years ago, wed, february 8, 2017, 12:00:00

I have problems when using named parameters in TypeScript. I know it is not supported the way I use it in TypeScript.


But how can I do it?


TypeScript:


SomeFunction(name1: boolean, name2: boolean, name3: boolean, name4: boolean) // Will occur only one 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):


Is there a way to provide named parameters in a function call 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 Visual Studio 2015 did not show a syntax error when using named parameter the way I used it in TypeScript...


PS.: I use TypeScript 2.1


More From » typescript

 Answers
26

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
}

[#59026] Monday, February 6, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cameron

Total Points: 591
Total Questions: 112
Total Answers: 88

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;