TypeScript's Basic Types: The Building Blocks
TypeScript builds on JavaScript's primitives (string, number, boolean) by letting you explicitly declare a variable's type. This is the foundation for catching errors early. The main footgun is confusing tuples [string, number] with flexible arrays.
Why it exists
JavaScript is dynamically typed, meaning a variable's type isn't known until the program runs. This can cause unexpected errors. TypeScript adds a static type system on top of JavaScript to help developers define what kind of data a variable should hold, catching type-related bugs during development.
The mental model
Think of TypeScript's basic types as explicit labels for your data containers. Instead of letting JavaScript infer a variable's type at runtime, you declare its intent upfront, such as let age: number;. This makes your code more predictable, self-documenting, and easier to reason about.
How it works
TypeScript supports the same primitive types as JavaScript, but allows you to explicitly annotate them. The core types are: boolean: A simple true/false value. let isDone: boolean = false; number: For all floating-point numbers, including decimal, hex, binary, and octal literals. let decimal: number = 6; bigint: For integers larger than the number type can safely handle. let big: bigint = 100n; string: For textual data, using single quotes, double quotes, or backticks for template strings with embedded expressions like ${expr}. let color: string = "blue"; Array: A list of elements of the same type, written as number[] or Array<number>. Tuple: An array with a fixed number of elements whose types are known but can differ. let x: [string, number]; x = ["hello", 10];
When to use it
Basic types are the foundation of any TypeScript application. You use them to declare variables, define function parameters, and specify return values. Use primitives (string, number, boolean) for simple values. Use arrays for variable-length lists of a single type. Use tuples for fixed-length structures where element position and type are both important.
When not to use it
Don't use a tuple when you need a variable-length list of items of the same type; an array is the correct tool for that job. For example, use number[] for a list of scores, not a tuple. Using a tuple incorrectly restricts you to a fixed length and type order where you need flexibility.
One canonical example
This example shows how string and number types work together using a template literal.
let fullName: string = Bob Bobbington;
let age: number = 37;let sentence: string = Hello, my name is {fullName}. I'll be {age + 1} years old next month.;
Interview question
When would a TypeScript tuple be the most appropriate choice compared to an array?
- a.To store a collection of user IDs, where the number of IDs can vary.
- b.To hold a series of sensor readings over time.
- c.To maintain a list of all available product categories.
- d.To represent a single point in 2D space, like [x-coordinate, y-coordinate].Correct
Why? this is the answer
A tuple is ideal for fixed-length structures where the position and type of each element are important, such as a coordinate pair. An array is used for variable-length lists of elements of the same type, making options A, C, and D incorrect.
Just read this? Test yourself on what you have been reading.
Read the original → typescriptlang.org
- #typescript
- #javascript
- #types
- #data types
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.
See open roles