Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

TypeScript with React: Build Your First Typed Components

Start building typed React components with a framework-aware setup, .tsx files, clear props, inferred Hook types, and practical event-handling guidance.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can learn TypeScript and React together: start with a framework that supports TypeScript, or add TypeScript to an existing app. For your first components, focus on a few essentials: put JSX in .tsx files, define the props a component accepts, and let React’s types help check Hooks and event handlers. TypeScript checks your code during development; it does not validate external data at runtime.

Choose a setup path

React’s documentation says that production-grade React frameworks support TypeScript and recommends following the setup instructions for your chosen framework. That is the clearest route for a new project because the framework’s configuration determines how TypeScript and JSX are handled. See React’s TypeScript guide for its framework guidance.

As an Amazon Associate I earn from qualifying purchases.

For an existing React project, React’s guide shows installing the React type definitions as development dependencies:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
npm install --save-dev @types/react @types/react-dom

Use that command in the context of the project’s existing toolchain; it does not replace any framework-specific TypeScript setup.

Put JSX in .tsx files and configure JSX handling

Every file that contains JSX needs the .tsx extension. TypeScript also needs a JSX option in tsconfig.json. For a React web application, React’s guide says the TypeScript lib setting should include dom. The appropriate JSX mode depends on the build pipeline: preserve leaves JSX for another transform, react emits React.createElement calls, and react-jsx emits calls to the automatic JSX runtime. TypeScript also documents react-jsxdev and react-native modes.

React says preserve is sufficient for most applications and advises libraries to consult the JSX documentation. Do not choose an emit mode by habit: use the option required by your framework or the tool that transforms JSX. The full set of options and their behavior is in the TypeScript JSX handbook.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • 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

Type a component’s props

A prop is an input passed to a component. For a small component, you can describe its props inline. A named type or interface is easier to read and reuse as the shape grows.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
interface GreetingProps {
  name: string;
}

function Greeting({ name }: GreetingProps) {
  return <h1>Hello, {name}!</h1>;
}

Here, name is required and must be a string wherever Greeting is used. The type also helps editors show the component’s expected inputs. You generally do not need to add a return-type annotation just for formality when TypeScript can infer the result.

Let Hooks infer types when they can

React’s type definitions cover built-in Hooks. When the initial value makes the state’s type clear, useState can infer it:

import { useState } from 'react';

function Toggle() {
  const [enabled, setEnabled] = useState(false);

  return (
    <button onClick={() => setEnabled(!enabled)}>
      {enabled ? 'On' : 'Off'}
    </button>
  );
}

Because the initial value is false, TypeScript infers a boolean state. When the initializer alone cannot express the intended type, provide a type argument. For example, an initially empty array that will contain strings can be written as useState<string[]>([]).

Use the editor to identify event types

React event handlers are typed, but the best event type depends on the element and handler. Rather than guessing, write the handler in the JSX attribute and hover over its parameter in your editor to inspect the inferred type. If no more specific event type is included, React’s guide identifies React.SyntheticEvent as the base type.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function NameField() {
  function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
    console.log(event.currentTarget.value);
  }

  return <input onChange={handleChange} />;
}

The example uses an input change event. In other handlers, inspect the type for the particular JSX attribute rather than copying this event type indiscriminately.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a children type that matches the component

Use React.ReactNode when a component accepts broadly renderable content, such as text or JSX. Use React.ReactElement when the prop specifically needs to be a JSX element.

Type Use it when What it does not guarantee
React.ReactNode The component can accept a broad range of renderable children. It does not limit children to a particular JSX tag.
React.ReactElement The component requires a JSX element as its child prop. It does not limit that element to a specific tag such as <li>.
interface PanelProps {
  children: React.ReactNode;
}

function Panel({ children }: PanelProps) {
  return <section>{children}</section>;
}

Type inline styles when you need them

React documents React.CSSProperties for a style object. It gives the object a suitable type while you define or reuse it:

const panelStyle: React.CSSProperties = {
  padding: '1rem',
  backgroundColor: 'white',
};

Know what TypeScript does—and does not—check

TypeScript describes the shapes and values expected by your code and checks them during development. A type annotation does not inspect an API response or otherwise validate data at runtime. If your application relies on external data being a particular shape, validate it at runtime before treating it as that type.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where to continue learning

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.