DevAcademy
LearnTypeScriptBasic Types
BeginnerTypeScript

Basic Types

Learn TypeScript’s core primitive types: string, number, boolean, null, and undefined.

Reading Time

14 min

Lesson

Lesson 3 of 30

Annotating Variables

A type annotation follows a variable name with a colon and the type, telling TypeScript exactly what values are allowed.

Basic Type Annotations

let username: string = "devacademy";
let age: number = 25;
let isActive: boolean = true;

username = 42; // Error: Type 'number' is not assignable to type 'string'

Primitive Types

TypeExample Values
string"hello", 'world', `template`
number1, 3.14, -10 (no separate int/float type)
booleantrue, false
nullnull
undefinedundefined
bigint100n
symbolSymbol("id")

One number Type for All Numbers

Unlike some languages, TypeScript doesn’t distinguish between integers and floats — every numeric value, whole or decimal, is typed as number.

Numbers

let price: number = 19.99;
let quantity: number = 3;
let hex: number = 0xff;

null and undefined

With strictNullChecks enabled (part of strict mode), null and undefined are their own distinct types and are not automatically assignable to other types unless explicitly included, e.g. string | null.

strictNullChecks in Action

let middleName: string | null = null; // must explicitly allow null
let firstName: string = null; // Error, if strictNullChecks is on

Arrays of Primitives

Arrays are typed by appending [] to the element type, e.g. number[] for an array of numbers.

Typed Arrays

let scores: number[] = [10, 20, 30];
let names: string[] = ["Alice", "Bob"];

Type Annotations Are Often Optional

TypeScript can usually infer the type from the initial value, so let age: number = 25 and let age = 25 behave identically. Explicit annotations matter most for function parameters and empty declarations.

Best Practice

Let TypeScript infer types for simple local variables, but always annotate function parameters explicitly — inference can’t know what a caller might pass in.

Interview Questions

Quick Quiz

1. How many distinct numeric types does TypeScript have for integers and decimals?

2. How do you type an array of strings?

3. What does strictNullChecks do?