Typing Immediately Destructured Parameters
Of course, TypeScript offers a way to provide an explicit type annotation. It's a little verbose, yet (if you think about it) consistent:
function toJSON (value : any , { pretty } : { pretty : boolean }) { const indent = pretty ? 4 : 0 ; return JSON . stringify (value, null , indent); }
If you want to change selection, open document below and click on "Move attachment"
Typing Destructured Object Parameters in TypeScript — Marius Schulzfile (set aside type errors, that is). Therefore, TypeScript can't simply change the meaning of the destructuring expression { pretty: boolean }. It looks like a type annotation, but it's not. #<span>Typing Immediately Destructured Parameters Of course, TypeScript offers a way to provide an explicit type annotation. It's a little verbose, yet (if you think about it) consistent: function toJSON(value: any, { pretty }: { pretty: boolean }) { const indent = pretty ? 4 : 0; return JSON.stringify(value, null, indent); } You're not directly typing the pretty property, but the settings object it belongs to, which is the actual parameter passed to the toJSON function. If you now try to compile the above T Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details