Get AI summaries of any video or article — Sign up free
How to use TypeScript with React... But should you? thumbnail

How to use TypeScript with React... But should you?

Fireship·
4 min read

Based on Fireship's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

TypeScript is a superset of JavaScript, so React code can remain familiar while gaining optional type-checking features.

Briefing

TypeScript’s biggest practical value in React isn’t that it changes how components run—it’s that it adds a compile-time type system that catches many common mistakes before the app ever ships. The core workflow stays familiar: TypeScript is a superset of JavaScript, so the code you write looks like JavaScript, but a compiler (configured via tsconfig) transpiles it into browser-ready JavaScript. That means teams can keep modern syntax and features while still targeting older environments, much like Babel already does in JavaScript-only React setups.

The transcript draws a clear line between what TypeScript can prevent and what it can’t. On the “catch bugs early” side, it highlights a typical runtime failure: starting with an empty object, then later accessing a deeply nested property that doesn’t exist. In plain JavaScript, that can slip through and crash in production. With TypeScript, the mismatch can be flagged during development—described as a “precog” that warns before the app runs. But the benefits aren’t a replacement for testing; TypeScript can’t guarantee correctness for logic errors (“batlogic”) or cover every scenario, so it complements rather than substitutes for test-driven development.

Where TypeScript becomes most tangible is its type system for React components and props. React components in TypeScript can be typed using FC (function component), which gives the compiler a concrete shape for the component. Props then stop being an unstructured “any” and become an explicit contract. The transcript walks through defining a props interface (e.g., cool props with a number and a string, with options for required vs optional fields). Once that interface is wired into the component type, editors provide autocomplete and enforce that required props are present—turning many “silly” integration mistakes into compile-time errors.

That comes with trade-offs. Adding types introduces boilerplate and friction, especially at the start, and the transcript notes that developers can bypass the system entirely by using any, which removes the safety net. There’s also a configuration cost: tsconfig can include strictness settings (with strict mode recommended) and a Target that controls which JavaScript version TypeScript outputs. The payoff is smoother refactoring and safer collaboration, particularly when multiple developers touch the same code.

Finally, the transcript frames the decision as a balance of confidence versus effort. It references survey claims attributed to Microsoft and Airbnb about the percentage of bugs TypeScript catches, then contrasts that with the real-world alternatives: manual documentation versus editor-driven intellisense, faster initial coding versus less chaotic refactoring later. The takeaway is that TypeScript’s value in React comes from turning implicit assumptions—about data shapes, props, and state—into enforceable contracts during development, while still requiring tests for full correctness.

Cornell Notes

TypeScript is a superset of JavaScript, so React code can look familiar while gaining a compile-time type system. A compiler configured by tsconfig transpiles TypeScript into browser-ready JavaScript, making modern syntax usable across environments. In React, typing components with FC and defining props via interfaces replaces vague any props with explicit contracts, enabling autocomplete and catching missing/incorrect props early. TypeScript can prevent certain runtime crashes (like accessing nested properties on an empty object), but it doesn’t replace testing for logic errors. The main trade-off is added boilerplate and stricter development workflow, which can be mitigated by using inference where possible and enabling strict mode for long-term maintainability.

How can TypeScript be “optional” to learn if it’s not a new language?

TypeScript is a superset of JavaScript, meaning vanilla JavaScript is valid TypeScript. The main difference is that TypeScript adds optional features—especially type annotations and a type-checking layer—on top of normal JavaScript syntax. The browser can’t run TypeScript directly, so a compiler transpiles it into standard JavaScript.

Why does tsconfig matter in a React + TypeScript project?

tsconfig customizes the TypeScript compiler’s behavior. The transcript highlights two practical categories: strictness (e.g., strict mode set to True to make type checking more rigorous) and Target (the JavaScript “flavor” TypeScript outputs, such as compiling to ES5 while allowing the codebase to use newer syntax like 2021). When strictness is higher, code may be harder to write at first, but refactoring and collaboration become safer.

What kind of bug does TypeScript catch that JavaScript often ships to production?

A common example is starting state as an empty object and later accessing a deeply nested property that doesn’t exist. JavaScript may compile and run until runtime, where it crashes. TypeScript can flag the mismatch during development—described as warning before the app runs—reducing the chance of runtime property-access failures.

How do FC and props interfaces change React development?

Typing a component with FC (function component) and defining props with a TypeScript interface turns props from vague any into an explicit contract. The transcript’s interface example (cool props with a number and a string, with required vs optional fields) enforces correct prop usage in JSX and enables editor autocomplete/intellisense for component consumers.

What are the main downsides or escape hatches of TypeScript?

TypeScript adds boilerplate and can slow initial development due to stricter checks. Developers can also bypass the system by using any, which opts out of type safety and negates many benefits. Additionally, tsconfig introduces configuration complexity when things don’t “just work.”

Does TypeScript replace testing?

No. The transcript emphasizes that TypeScript helps detect poorly structured code and certain categories of mistakes before runtime, but it can’t detect logic errors (“batlogic”). Testing remains necessary for correctness beyond type safety.

Review Questions

  1. What does it mean that TypeScript is a superset of JavaScript, and what role does the compiler play for browser execution?
  2. How do strict mode and Target in tsconfig affect development and compatibility?
  3. In what ways do FC and props interfaces improve correctness and developer experience in React?

Key Points

  1. 1

    TypeScript is a superset of JavaScript, so React code can remain familiar while gaining optional type-checking features.

  2. 2

    A TypeScript compiler transpiles code into browser-ready JavaScript, with tsconfig controlling strictness and output Target.

  3. 3

    Enabling strict mode (strict: True) increases early friction but improves refactoring safety and team collaboration.

  4. 4

    TypeScript can catch certain runtime crash patterns during development, such as invalid nested property access on incorrectly typed values.

  5. 5

    React props become safer when typed explicitly (e.g., using FC and props interfaces) instead of defaulting to any.

  6. 6

    TypeScript adds boilerplate and can be undermined by opting out with any, so teams must use types intentionally.

  7. 7

    Type safety complements testing; it doesn’t replace tests for logic errors.

Highlights

TypeScript doesn’t change what runs in the browser—it adds a compile-time layer, then transpiles to JavaScript via a compiler configured by tsconfig.
Typing React props with interfaces turns “mystery any” into an enforceable contract, enabling autocomplete and preventing missing/incorrect props in JSX.
TypeScript can prevent certain production crashes (like accessing nested properties on an empty object), but it can’t guarantee correct business logic.
The real decision is a trade-off between extra upfront boilerplate and long-term confidence during refactoring and collaboration.

Topics

  • TypeScript Basics
  • React Props Typing
  • tsconfig Strict Mode
  • Compile-Time Type Safety
  • Babel vs TypeScript

Mentioned

  • FC
  • TS
  • ES5