> For the complete documentation index, see [llms.txt](https://wiki.nushackers.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.nushackers.org/orbital/typescript/typescript-tricks-youll-use-all-the-time-in-react.md).

# TypeScript Tricks You’ll Use All the Time in React

## Destructuring with Types

You’ve probably used destructuring in [**JavaScript**](/orbital/readme-1.md):

```jsx
const user = { name: "Alice", age: 30 };
const { name } = user;
```

But in **TypeScript**, destructuring can be *typed* for extra safety:

```tsx
interface User {
  name: string;
  age: number;
}

const user: User = { name: "Alice", age: 30 };

const { name, age }: User = user; // Typed destructuring

console.log("name: ", name); // "name: Alice"
console.log("age: ", age); // "age: 30"
```

You can also destructure in function parameters:

```tsx
function greet({ name }: { name: string }) {
  console.log(`Hi, ${name}`);
}
```

Even better, use `interfaces` to keep things clean:

```tsx
interface GreetProps {
  name: string;
}

function greet({ name }: GreetProps) {
  console.log(`Hi, ${name}`);
}
```

> ✅ You will use this very often in React component props!

## Spread Operator with Typed Objects

### &#x20;What is the Spread Operator?

The spread operator (...) is a feature in **JavaScript** (and supported in **TypeScript**) that allows you to “spread” the properties of an object or elements of an array into another object or array.

```tsx
const user = { name: "Alice", age: 25 };
const updatedUser = { ...user, age: 26 };
```

* `...user` spreads all the properties of user into a new object.
* We then **override** the age property.

This is especially useful when mutating objects immutably, i.e., creating a *new* object based on the old one but with some changes — a core concept in React state updates.

> #### :brain: Use the spread operator instead of direct mutation to keep your data.&#x20;
>
> This helps React detect changes, triggers proper re-renders, and avoids unexpected side effects.

Let's say you want to update a specific property of the object. You will do the following:

```tsx
interface User {
  name: string;
  age: number;
}

const [user, setUser] = useState<User>({ name: "Alice", age: 25 });

setUser(prev => ({ ...prev, age: 26 })); // only update the age to 26 here.
```

## Default Values with Destructuring

> Destructuring + default values = cleaner, safer code.

```tsx
function greet({ name = "Guest" }: { name?: string }) {
  console.log(`Hi, ${name}`);
}

greet({ name: undefined }); // "Hi, Guest"
greet({ name: "Bob" }); // "Hi, Bob"
```

## Rename in Destructuring

You can rename variables while destructuring:

```tsx
const user = { name: "Alice" };
const { name: userName } = user;

console.log(userName); // "Alice"
```

## Typing Arrays and Spread

You can spread arrays too, and TypeScript keeps the types intact.

```tsx
const todos: string[] = ["Buy milk"];
const updatedTodos = [...todos, "Walk dog"]; // Type is still string[]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wiki.nushackers.org/orbital/typescript/typescript-tricks-youll-use-all-the-time-in-react.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
