Skip to main content
CodeOath

Front-end and full stack · Chapter 5

TypeScript for the front end: types that catch real bugs

About this chapter

TypeScript checks a program before it runs and then disappears, which explains both what it can catch and what it cannot. This chapter explains what the compiler checks and erases, when to write a type and when to let inference do it, how types describe shapes, and how unions with narrowing, literal types, generics and the unknown type catch the bugs that real front-end data causes.

Who it is for: Front-end developers who write JavaScript and want to use TypeScript with a clear idea of what it guarantees and what it leaves to run time.

You will be able to

  • Say what the TypeScript compiler checks and what disappears when the code is compiled.
  • Decide when to write a type annotation and when to rely on inference.
  • Use unions and narrowing to handle a value that may be one of several types.
  • Use a union of literal types in place of an enum for a closed set of values.
  • Treat data from a server as unknown and narrow it, instead of switching checking off with any.

The key idea: Types exist only while the code is built, so they protect the code you wrote and say nothing about the data that arrives later. Safety comes from describing data honestly at the edges and letting the compiler check everything inside.

In this chapter

  1. What the compiler does, and what it does not
  2. Inference, and when to annotate
  3. Shapes, not names
  4. Functions: optional parameters and overloads
  5. Unions and narrowing
  6. Literal types, as const and enums
  7. Generics, constraints, keyof and typeof
  8. unknown, any, never and assertions
  9. Mistakes that recur

Locked: needs a plan

You can read chapter 1 free. Chapter 5 is the next one.

What you can do now, free: re-read chapter 1 and try its problems.

What a plan gives you: all chapters of the track you pick and their checks. Professional, one role is ₹329 a month or ₹2,299 a year. It renews until you cancel, and you can cancel any time. Paid plans open at launch.

Why is this locked?

Chapter 1 of every track is free, so you can see how the track reads. The rest of a track and its checks come with a plan, or open as you pass each chapter's check.

The price is for one period, a month or a year. A plan renews until you cancel.

Back to the track