Difficulty: Beginner
TypeScript builds on JavaScript's primitive types by allowing you to explicitly declare which type a variable holds. The seven primitive types in TypeScript mirror JavaScript: `string`, `number`, `boolean`, `null`, `undefined`, `bigint`, and `symbol`. When you annotate a variable with a primitive type, TypeScript ensures that only values of that type can be assigned to it throughout its lifecycle.
The `string` type covers all text values, whether created with single quotes, double quotes, or template literals. The `number` type encompasses all numeric values - integers, floats, `Infinity`, and `NaN` (yes, NaN is of type number). The `boolean` type is limited to `true` and `false`. These three are the most commonly used primitives in day-to-day TypeScript development.
The `null` and `undefined` types are special. With `strictNullChecks` enabled (which is the default under `strict: true`), `null` and `undefined` are their own distinct types and cannot be assigned to other types without an explicit union. This is one of TypeScript's most valuable features: it forces you to handle cases where a value might not exist, preventing the infamous "Cannot read property of null" errors.
The `bigint` type represents arbitrarily large integers (created with the `BigInt()` function or the `n` suffix like `100n`). The `symbol` type represents unique identifiers created with `Symbol()`. Both are less commonly used but important in specific contexts - `bigint` for large number arithmetic and `symbol` for creating unique object property keys that won't collide with other properties.
const name: string = "TypeScript";
const version: number = 5.3;
const isTyped: boolean = true;
const nothing: null = null;
const notDefined: undefined = undefined;
console.log(`${name} ${version}, typed: ${isTyped}`);
console.log(`null is: ${nothing}, undefined is: ${notDefined}`);
Each variable is annotated with its primitive type. TypeScript will prevent you from reassigning a value of a different type to any of these variables.
const integer: number = 42;
const float: number = 3.14;
const negative: number = -10;
const notANumber: number = NaN; // NaN is still type 'number'
const infinite: number = Infinity;
console.log(typeof notANumber); // "number"
console.log(Number.isNaN(notANumber)); // true
const bigValue: bigint = 9007199254740993n;
console.log(bigValue.toString());
NaN and Infinity are both valid number values in TypeScript. BigInt handles integers larger than Number.MAX_SAFE_INTEGER accurately.
function findUser(id: number): string | null {
if (id === 1) return "Alice";
return null;
}
const user = findUser(1);
const missing = findUser(99);
// Must check for null before using string methods
if (user !== null) {
console.log(user.toUpperCase());
}
console.log(missing === null ? "No user found" : missing);
With strictNullChecks, the return type `string | null` forces callers to handle the null case. This prevents runtime null reference errors.
string, number, boolean, null, undefined, bigint, symbol