Basic Types, Arrays and Tuples
Types describe what values a variable can hold. You'll write some yourself, but TypeScript infers most simple ones from the values you assign. The real value of types shows up when data moves between functions and files.
The everyday types
Example
Type inference
You don't have to write every type. If you give a value straight away, TypeScript infers the type for you:
Example
Tuples: fixed-length arrays
Example
any and unknown
anyswitches type checking off for that value. Avoid it - it hides bugs.unknownmeans "could be anything, check before you use it". It's the safe choice for data from outside (user input, an API).
Example
Let TypeScript infer simple variables. Write types on function parameters and on data shapes - that's where they help most.
Key points
- Basic types: string, number, boolean, arrays, tuples.
- TypeScript infers simple types.
- Prefer unknown over any for untrusted data.
📝 Test yourself
1. How do you write the type 'array of numbers'?
2. Which type is safer for data you haven't checked yet?
3. Fill in a tuple type for [name, price]:
const item: [string, ] = ["Pen", 10];
