NUS Hackers Wiki
NUS Hackers Wiki
  • NUS Hackers Wiki
  • Hackerschool
    • Virtual Machines and Linux
    • Beginners' Guide to the Terminal
      • Introduction to the Terminal
      • Modern Shell Tools
      • Shell Scripting
      • Real World Scripting
      • Resources
    • Self-Hosting: Three Easy Pieces
      • 1. Setting up your server
      • 2. Running Services
      • 3. Monitoring your server
    • Vim
    • Introduction to Zig
      • Language Basics
      • Error Handling
      • Memory Management
      • Working with C
      • Exploring comptime
    • CI/CD with Github Actions
      • Background
      • Basics of Github Actions
        • Target workflow
        • Running unit tests
        • Linting code
        • Deploying to Github Pages
      • Advanced use cases
        • Pollers
        • Github script
        • Executing third-party scripts
        • Reusable workflows
      • Cookbook
    • Lightning Git
      • Git Concepts
      • Getting Started with Git
      • Making your first commit
      • Branching
      • Merge Conflicts
      • Integrating remote repositories
      • Collaborative Workflows
      • Commit Manipulation and Reflog
      • Interactive rebasing
      • filter-repo
  • Orbital
    • JavaScript
      • Browser Developer Tools
      • Getting Started
      • Datatypes
      • Operators and Operations
      • Loops and Conditions
      • Functions
      • Strings
      • Arrays
      • HTML
        • Getting Started
        • Tag Attributes
        • HTML Forms
        • Browser Inspector
      • CSS
        • Selectors
        • Colors in CSS
        • Measurements in CSS
        • The Box Model
        • Adding Styles - Part 1
        • Adding Styles - Part 2
      • Working with the DOM
        • Querying the DOM - Selectors
        • Querying the DOM - Element Attributes
        • Querying the DOM - Element Styles
        • Events with JS and HTML
        • Exercise: Click Counter
        • Editing the DOM
        • Fetch Requests
        • Exercise: The NUSMods API
    • React
      • Setup
      • State
    • React Native
      • Setup
      • Intro to JSX
      • Basic Syntax
      • Handling UI
      • Props
      • State Management
    • Git
      • Setup
      • Command Glossary
      • Fundamental Concepts
        • Getting Started
        • Integrating Remote Repositories
        • Branching
        • Merge Conflicts
      • Collaborative Workflows
        • Fork and PR Workflow
        • Branch and PR Workflow
      • Advanced Concepts
        • Ignoring Files
        • Commit Message Conventions
        • Github Collaborators
        • CI/CD with Github Actions
        • Advanced Git Commands
      • FAQ
    • Telegram Bot
      • Creating a TeleBot
      • API Calls
      • Telebot Basics
      • Integrating API's
    • Relational Database
      • Database Overview
      • Database Design
      • Entity Relationship Diagram
      • SQL Basics & PostgreSQL
    • TypeScript
      • Types and Interfaces
      • Utility Types
      • Typing Component Props, Events, and Hooks
      • Why You Should Avoid Using any (and What to Do Instead)
      • TypeScript Tricks You’ll Use All the Time in React
Powered by GitBook
On this page
  • Destructuring with Types
  • Spread Operator with Typed Objects
  • What is the Spread Operator?
  • Default Values with Destructuring
  • Rename in Destructuring
  • Typing Arrays and Spread
Edit on GitHub
Export as PDF
  1. Orbital
  2. TypeScript

TypeScript Tricks You’ll Use All the Time in React

PreviousWhy You Should Avoid Using any (and What to Do Instead)

Last updated 2 days ago

Destructuring with Types

You’ve probably used destructuring in :

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

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

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:

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

Even better, use interfaces to keep things clean:

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

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.

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.

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:

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.

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:

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.

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

Use the spread operator instead of direct mutation to keep your data.

🧠
JavaScript