Primitive Types: string, number, and boolean
Every JavaScript value has a type. TypeScript names three primitives that cover the vast majority of values you work with: string, number, and boolean. This lesson covers each one in depth, then introduces how strict: true changes what null and undefined mean.
string
Section titled “string”A string is any sequence of characters. TypeScript enforces that string methods are only called on strings and that strings are not passed where numbers are expected:
const category: string = 'Science & Nature';const question: string = 'What planet is closest to the Sun?';
console.log(category.toUpperCase()); // 'SCIENCE & NATURE'console.log(category.length); // 19TypeScript catches misuse immediately:
const score: string = 'ten';score.toFixed(2); // Error: Property 'toFixed' does not exist on type 'string'This is exactly the error JavaScript would throw at runtime — TypeScript surfaces it at write time.
number
Section titled “number”JavaScript has a single number type for both integers and floating-point values. TypeScript reflects this:
const questionCount: number = 10;const passingRate: number = 0.6;const score: number = 7;Arithmetic works as expected. TypeScript catches calls to string methods on numbers and vice versa:
const amount: number = 45.5;console.log(amount.toFixed(2)); // '45.50' — this is fine, toFixed is a number methodconsole.log(amount.toUpperCase()); // Error: Property 'toUpperCase' does not exist on type 'number'Note that toFixed returns a string. TypeScript knows this — if you try to do arithmetic on the result, you get an error.
boolean
Section titled “boolean”A boolean is exactly true or false. With strict: true, TypeScript does not treat other values as “truthy” or “falsy” in type position — 0, '', and null are not assignable to boolean:
const isDone: boolean = false;const isCorrect: boolean = true;
let flag: boolean = 0; // Error: Type 'number' is not assignable to type 'boolean'This strictness prevents a class of subtle bugs where developers pass numeric flags or empty strings in places expecting true booleans.
Strict null checks
Section titled “Strict null checks”With strict: true enabled, null and undefined are not part of string, number, or boolean. They are separate types. This means a variable annotated as string cannot be null:
let name: string = null; // Error: Type 'null' is not assignable to type 'string'If a value can be absent, you say so explicitly with a union type:
let name: string | null = null; // ✓ — explicitly allows nullname = 'AceIt'; // ✓ — string is also validThis one rule prevents the most common JavaScript runtime error: Cannot read properties of null. TypeScript forces you to acknowledge the possibility of null before you use the value.
AceIt’s primitives
Section titled “AceIt’s primitives”AceIt uses all three primitives throughout its source files:
// From quiz.tsconst score: number = 7;const total: number = 10;const isDone: boolean = false;
// From types.tsconst question: string = 'What planet is closest to the Sun?';const category: string = 'Science & Nature';When you call engine.answer(selected) and it returns a boolean, TypeScript knows the return value can only be true or false — never undefined, never 0. That guarantee flows through the entire app.
Exercise
Section titled “Exercise”In a new file primitives.ts, write the following with explicit type annotations:
- A
stringvariable holding a trivia category name of your choice. - A
numbervariable holding a score (e.g.7). - A
booleanvariableisNewHighScoreset tofalse. - A function
describeResult(score: number, total: number): stringthat returns a sentence like"You got 7 out of 10". - Try assigning
nullto yourstringvariable. Read the error, then fix it by usingstring | null.
Compile with npx tsc primitives.ts --strict --noEmit and resolve all errors.
string,number, andbooleanare the three core primitive types.- TypeScript enforces that methods belonging to one type are not called on another.
- With
strict: true,nullandundefinedare not assignable to primitive types — they must be declared explicitly with a union type. - Strict null checks prevent the most common JavaScript runtime crash: accessing a property on
null.