To destructure properties from a DOM element in TypeScript, first make sure the element exists, then use ordinary object-destructuring syntax: const { id, title } = element;. The element’s static type determines which properties are available and the types of the local bindings.
How to destructure properties from a DOM element
Object destructuring reads named properties from an object into local variables. For example, const { id, title } = element; is equivalent to reading element.id and element.title into separate bindings. It does not deep-copy the element or its nested values.
As an Amazon Associate I earn from qualifying purchases.
A DOM lookup may not find a match, so check for a result before accessing or destructuring it:
const element = document.getElementById('profile');
if (element) {
const { id, title } = element;
console.log(id, title);
}
getElementById has the return type HTMLElement | null. The if check narrows the type inside the block and prevents destructuring a null value, which would throw at runtime. TypeScript’s DOM Manipulation documentation describes the DOM typings and lookup return types.
#1 Best Overall
Choose a specific element type when you need specific properties
An ID lookup gives you a general HTMLElement. That type exposes general element properties, but not every property available on a particular tag. If you need input-specific properties such as value or disabled, select an input or narrow to its specific interface:
const input = document.querySelector('input');
if (input) {
const { value, disabled } = input;
console.log(value, disabled);
}
With the tag selector, TypeScript can infer an input element type, and the null check handles the possibility that no input matched. Tag-specific DOM interfaces also apply to elements such as HTMLAnchorElement and HTMLParagraphElement. Prefer a suitably specific selector or a justified narrowing step over casting a generic HTMLElement just to make a property accessible.
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
Rename a property or supply a default
Use propertyName: localName to give a destructured property a different local variable name. The name before the colon is the property; the name after it is the new binding:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
const { id: elementId } = element;
A default value applies when the property value is undefined:
const { title = 'Untitled' } = element;
This default does not make a missing source object safe: the element must still be checked before destructuring. Nor does it replace an explicit null; handle null separately if the property’s type includes it. The TypeScript variable declaration guide and MDN’s destructuring assignment reference cover renaming and defaults.
Put type annotations after the complete pattern
When destructuring a typed object in a function parameter, place the type annotation after the closing brace of the pattern—not inside it:
type Profile = {
id: string;
title: string;
};
function showProfile({ id, title }: Profile) {
console.log(id, title);
}
The same placement applies with an inline object type: function handle({ id }: { id: string }) {}. A named type is generally easier to maintain when the shape has several properties. See TypeScript’s More on Functions and Object Types documentation.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteKeep null checks and later DOM work clear
Destructuring is most useful when it makes repeated property reads simpler. Keep the element reference when later code still needs DOM operations, or when retaining it makes the null check easier to follow. Avoid packing deep nesting, several renames, and defaults into one expression; a few explicit lines are often easier to understand.
Best Value
Optional chaining can safely read a property, but it does not itself create a destructured binding. If you want local variables, guard the lookup and destructure inside the guarded block. A non-null assertion such as element! only suppresses TypeScript’s warning; use it only when the surrounding code genuinely guarantees that the element exists.
DOM properties are not always spelled like HTML attributes
Destructuring uses the property names exposed by the element’s TypeScript DOM interface. Do not assume an HTML attribute and its DOM property always have identical spelling. Check the interface for the specific element and property you need; historical DOM specifications can explain naming conventions, but they are not a current compatibility reference.
Assigning into existing variables
If the variables already exist, use assignment destructuring rather than a declaration. Parenthesize the object pattern so JavaScript does not interpret its opening brace as the start of a block:
let id: string;
({ id } = element);
For most DOM lookups, declaring the bindings inside the successful null check is simpler and keeps their scope limited to where the element is known to exist.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




