Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which type of value in the object as parameter of function. Why do not have the compilation errors?

I have gotten the next bunch of code.

const func: ( newState: { newState: number }) => void = ({ newState: newState }) => {
    console.log(newState);
}

For me, particularly interesting is the ({ newState: newState }) how is it work?
Why can I write newState: newState in this situation, and no compilation errors?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

 const func: ( labeledParameter: { newState: number }) => void = ({ newState: test }) => {
    console.log(test);
}

func({newState: 12});

It’s because the first newState is a label put on the parameters give to the function -> i renamed it labeledParameter in my sample

the object

{ newState: newState }

is an object with a property newState and as value a number

to call the function you should use

func({newState: 12});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading