Skip to content

TypeScript Basic Types: string, number, boolean, Arrays, Inference and null

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

In JavaScript, "20" * 3 gives 60 but "20" + 3 gives "203", and nothing warns you until a customer sees the wrong total. TypeScript adds one simple idea: say what kind of value each thing holds, and let a program check that you keep your word. This first lesson shows what a type error looks like, the handful of basic types you will use every day, how TypeScript works out most types on its own, and the one check that catches more beginner bugs than any other: a value that might be null. Every example runs the real TypeScript compiler, so edit the code and watch the errors appear and disappear in the Console box.

Quick answer

TypeScript adds type annotations after a colon: let city: string = "Dubai", const qty: number = 2, let paid: boolean = false, and const prices: number[] = [45, 30]. Annotate function parameters and, when it helps, the return type: function addVat(amount: number, rate: number): number. For most variables you can skip the annotation, because TypeScript infers the type from the value. Avoid any, which switches checking off; use unknown for values you have not checked yet. In strict mode, querySelector returns an element or null, so check it with if (el) before using it.

30 min read and practice · Content checked on 17 November 2026

In this lesson you will

  • Read a TypeScript error message and find the line and the cause
  • Annotate variables, arrays, parameters and return values with basic types
  • Let inference do the work and know when an annotation helps
  • Handle values that may be null, and choose unknown over any

Your first type error

A type is the kind of value something holds: text, a number, true or false, a list. When you write const qty: number = 2, the part after the colon is a type annotation. It is a promise: qty will always be a number. If you break the promise, for example by writing "2" with quotes, the compiler stops you with a message such as "Type 'string' is not assignable to type 'number'". Read it from the inside out: you tried to put a string where a number was promised.

Each message has a code, such as TS2322, and a line number. The codes are useful for searching, and the most common ones soon become familiar: TS2322 for a value of the wrong type, TS2345 for a wrong argument in a function call, TS2339 for a property that does not exist and TS18047 for a value that may be null. Notice in the example that the JavaScript still runs after the error, and prints the wrong total. The compiler warned you; it did not stop you. In a real project you fix every error before you publish.

Try it: The bug TypeScript catches
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

The Console shows TS2322 on line 5, then the JavaScript runs anyway and prints 203 instead of 23, because the string was glued on. Remove the quotes around 3 and press Run: the error disappears and the total is right.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is the difference between any and unknown in TypeScript?

Both accept any value. With any, TypeScript stops checking and lets you do anything with it, including mistakes. With unknown, you must first check the value, for example with typeof, before TypeScript lets you use it. Prefer unknown.

Is there an integer type in TypeScript?

No. Like JavaScript, TypeScript has one number type for whole numbers and decimals. For whole numbers larger than about 9 quadrillion there is bigint. Check for whole numbers at run time with Number.isInteger().

What does the exclamation mark after a variable mean in TypeScript?

It is the non-null assertion: el! tells TypeScript the value is not null or undefined. It removes the type error but adds no check, so if you are wrong the code still crashes. Use an if check instead whenever you can.

Why does the JavaScript still run when there are type errors?

By default the TypeScript compiler reports errors but still writes the JavaScript, so a type error never blocks you while you experiment. Projects usually stop the build on errors with the noEmitOnError option or a separate check step in their tests.

Do I have to rewrite my JavaScript project to use TypeScript?

No. Because every JavaScript file is valid TypeScript syntax, you can rename files from .js to .ts one at a time and add types gradually. The allowJs option lets both kinds of file live in the same project while you move over.

Sources

Facts in this lesson were checked against these official pages on 17 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.