Lesson 2 of 13 · 0 of 1 exercises solved
Basic Types
Learn TypeScript's basic types, including the extras it adds to JavaScript such as , enums, `unknown`, `never` and `void`. You will also see how TypeScript infers types so you do not have to write them everywhere.
// Primitive types
let name: string = "Alice"
let age: number = 30
let active: boolean = true
let nothing: null = null
let missing: undefined = undefined
// Type inference — TypeScript infers from assignment, annotation often optional
let inferredName = "Bob" // TypeScript knows this is string
let inferredAge = 25 // TypeScript knows this is number
// inferredAge = "twenty-five" // Error: Type 'string' is not assignable to 'number'
// Arrays
let nums: number[] = [1, 2, 3]
let strs: Array<string> = ["a", "b", "c"] // generic form
let mixed: (string|number)[] = ["a", 1, "b", 2]
// Tuples — fixed-length array with known types at each position
let point: [number, number] = [10, 20]
let entry: [string, number, boolean] = ["Alice", 30, true]
const [x, y] = point // destructuring works
// any — opt out of type checking (use sparingly)
let wild: any = "anything"
wild = 42
wild = true
wild.nonExistentMethod() // no error — you lose type safety
// unknown — safer alternative to any (must narrow before use)
let input: unknown = getUserInput()
if (typeof input === "string") {
console.log(input.toUpperCase()) // OK — narrowed to string
}
function getUserInput(): unknown { return "hello" }// Enums — named constants
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
function move(dir: Direction): void {
console.log(`Moving ${dir}`)
}
move(Direction.Up) // Moving UP
// move("UP") // Error: string is not Direction
// Const enums — inlined at compile time, no runtime object
const enum Status {
Pending, // 0
Active, // 1
Inactive, // 2
}
// void — function returns nothing
function log(msg: string): void {
console.log(msg)
}
// never — function never returns (throws or infinite loops)
function fail(msg: string): never {
throw new Error(msg)
}
// Type assertions — tell TypeScript you know better
const input = document.getElementById("name") as HTMLInputElement
const value = (input).valueCommon question
Does TypeScript make my program safer at run time?
TypeScript is JavaScript with a static type system on top. describe the shapes you intend values to have; the checks that the program uses them consistently, then strips them out and outputs plain JavaScript. The benefit is that whole categories of mistakes (a misspelled , a where a number was meant) are caught while you edit rather than in production.
Turn on strict mode, annotate signatures, let inference handle local , and use unknown for data that has not been checked yet. At the edges of the program (network, storage, user input) validate and convert, because that is where the compiler cannot see.
Source: The TypeScript handbook: "TypeScript's type system is erased at compile time. It does not add runtime checks."
Common mix-ups
Each of these sounds reasonable. Think about why it is wrong, then open the explanation.
A TypeScript program won't run if it has a type , so runtime failures are impossible.
Annotating a as number guarantees a number arrives.
any is a harmless escape hatch for when the types get awkward.
Practice
Annotate every declaration with an explicit type: a string, a number, a , an of strings, and a tuple of [string, number]. Then build summary so it reads Maya has 3 books.
Annotate the basics
const name = "Maya"
const books = 3
const isMember = true
const titles = ["Dune", "Emma"]
const pair = ["Paris", 1889]
const summary = ""
console.log(summary)
Press Run to try your code, then Check answer to grade it.Check yourself
What happens at runtime when TypeScript code with a type error is compiled with plain tsc and run?
Tip: use the ← and → keys to move between lessons.