Using named JavaScript parameters (based on typescript)

I am having problems using a named parameter in TypeScript, I know that it is not supported 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 looked (this did not work):

Named parameters in javascript

How to add optional named parameters to a TypeScript function parameter?

What can I do in TypeScript to use named parameters in JavaScript?

This is an example of using VisualStudio: here . Interestingly, VS2015 did not show a syntax error when using the named parameter as I used it in TypeScript ...

ps .: I am using TS 2.1

+4
2

:

interface Names {
    name1: boolean
    name2: boolean
    name3: boolean
    name4: boolean
}

function myFunction({name1, name2, name3, name4}: Names) {
    // name1, etc. are boolean
}

. Names . JavaScript ( ) TS:

function myFunction({name1, name2, name3, name4}) {
    // name1, etc. are of type any
}
+4

, " ", - :

type SomeFunctionParams = {
    name1: boolean;
    name2: boolean;
    name3: boolean;
    name4: boolean;
}

SomeFunction(params: SomeFunctionParams) { ... }

:

$(function () {
    SomeFunction({
        name1:false, 
        name2:false, 
        name3:false, 
        name4:true
    });  
});
+1

Source: https://habr.com/ru/post/1669135/


All Articles