TypeScript Tutorial: Basic Types
Primitive types, arrays, tuples, and special types like any, unknown, and never.
Primitive Types
```typescript
let name: string = "Alice";
let age: number = 30;
let price: number = 9.99;
let active: boolean = true;
let nothing: null = null;
let missing: undefined = undefined;
let id: bigint = 9007199254740991n;
let sym: symbol = Symbol("unique");
```
---
Arrays
```typescript
let nums: number[] = [1, 2, 3];
let names: string[] = ["Alice", "Bob"];
// Generic syntax (equivalent)
let scores: Array<number> = [100, 95, 87];
```
---
Tuples
Fixed-length arrays where each position has a known type:
```typescript
let point: [number, number] = [10, 20];
let entry: [string, number] = ["age", 30];
console.log(point[0]); // 10
console.log(entry[1]); // 30
```
---
Special Types
| Type | Meaning |
|---|---|
| `any` | Opt out of type checking — use sparingly |
| `unknown` | Like `any` but safe — must narrow before use |
| `never` | A value that never occurs (e.g. unreachable code) |
| `void` | Function that returns nothing |
| `object` | Non-primitive value |
```typescript
// unknown is safer than any — must check type before using
function process(input: unknown) {
if (typeof input === "string") {
console.log(input.toUpperCase()); // safe
}
}
```
---
Type Assertions
When you know more than TypeScript:
```typescript
const input = document.getElementById("email") as HTMLInputElement;
const value = input.value; // TypeScript now knows it's an input element
```
---
What's Next?
Next: **Interfaces** — how to define the shape of objects.
What you'll learn in this TypeScript basic types tutorial
This interactive TypeScript tutorial has 14 hands-on exercises. Estimated time: 12 minutes.
- The primitive types — string, number, boolean — Every value in TypeScript has a type. The three most fundamental are `string`, `number`, and `boolean`. TypeScript infer…
- null and undefined — the billion-dollar mistake — Tony Hoare called null "my billion-dollar mistake". TypeScript's strict mode separates `null` and `undefined` from other…
- any — the escape hatch (use sparingly) — `any` opts out of type checking entirely. It's useful when migrating JavaScript code or working with truly dynamic data …
- Template literal types — string patterns — Template literal types create new string types by combining string literals. They're used for route patterns, event name…
- Literal types — exact values — A literal type is a type that allows only one specific value. Combined with union types, literal types create powerful, …
- bigint — large integers — `bigint` handles integers larger than `Number.MAX_SAFE_INTEGER` (2^53 - 1). It's used in cryptography, database IDs, and…
- symbol — unique identifiers — Every `Symbol()` call creates a unique, non-comparable value. Symbols are used as unique property keys that can't accide…
- Type widening — const vs let — TypeScript widens types for `let` (reassignable) but keeps them narrow for `const` (fixed). This affects how types flow …
- Type assertions — when you know better — Sometimes TypeScript can't figure out the type from context, but you know. Type assertions (`as Type`) tell TypeScript t…
- Non-null assertion — telling TypeScript a value exists — The non-null assertion operator `!` tells TypeScript a value is definitely not null or undefined. It's a compile-time-on…
- Checking types at runtime — typeof and instanceof — `typeof` checks primitive types at runtime. `instanceof` checks if a value is an instance of a class. TypeScript narrows…
- Type predicates — custom narrowing functions — A type predicate `value is Type` in a function return type tells TypeScript to narrow the type when the function returns…
- Satisfies vs as — validation without widening — `as` asserts a type (bypasses checking). `satisfies` validates against a type but preserves the inferred type. Both are …
- Branded types — prevent value mix-ups — Branded types prevent mixing up values of the same underlying type. A `UserId` and an `OrderId` are both `number`, but y…
TypeScript Basic Types concepts covered
- Primitive Types
- Arrays
- Tuples
- Special Types
- Type Assertions
- What's Next?