Skip to content

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.

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); // 19

TypeScript 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.

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 method
console.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.

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.

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 null
name = 'AceIt'; // ✓ — string is also valid

This 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 uses all three primitives throughout its source files:

// From quiz.ts
const score: number = 7;
const total: number = 10;
const isDone: boolean = false;
// From types.ts
const 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.

In a new file primitives.ts, write the following with explicit type annotations:

  1. A string variable holding a trivia category name of your choice.
  2. A number variable holding a score (e.g. 7).
  3. A boolean variable isNewHighScore set to false.
  4. A function describeResult(score: number, total: number): string that returns a sentence like "You got 7 out of 10".
  5. Try assigning null to your string variable. Read the error, then fix it by using string | null.

Compile with npx tsc primitives.ts --strict --noEmit and resolve all errors.

  • string, number, and boolean are the three core primitive types.
  • TypeScript enforces that methods belonging to one type are not called on another.
  • With strict: true, null and undefined are 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.