OneBite.Dev - Coding blog in a bite size

How to declare type in Object destructuring typescript

Learn how to declare type in typescript when using Object destructuring in Javascript

Learn how to declare type in typescript when using Object destructuring in Javascript.

At first, I tried using semicolon(:) and type but it keeps showing me an error.

function TextElement({el: TextEl, text: string}){
..
}

What’s wrong with above is, in Javascript putting semicolon on object desctructuring means assign the value to it. So it’ll find TextEl variable and assign to El. That’s not what we want!

What we should do to be able to use typescript in “Javascript Object Desctrucure” is the following:

function TextElement({el, text}: {el: TextEl, text: string}){
..
}

Hope it helps!

typescript