0Pricing
TypeScript Academy · Lesson

Primitive Types: string, number, boolean

Annotate variables with TypeScript's three core primitives.

Primitive Types: string, number, boolean is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

TypeScript's three core primitive types are string, number, and boolean. In this lesson you'll learn how to annotate variables with these types.

Annotating Variables

Add a type annotation after the variable name with a colon. TypeScript will enforce the type for the lifetime of the variable.
let username: string = 'Alice';
let score: number = 100;
let isLoggedIn: boolean = true;

Type Inference

TypeScript can infer types from the initial value. You don't always need an explicit annotation — the compiler figures it out.
let city = 'Istanbul';   // inferred as string
let price = 9.99;        // inferred as number
let enabled = false;     // inferred as boolean

Type Errors at Compile Time

Once a variable has a type, assigning a different type is a compile-time error. TypeScript catches this before the code runs.
let count: number = 10;
count = 'ten'; // Error: Type 'string' is not assignable to type 'number'

The string Type

The string type represents all text values. TypeScript checks that string operations (like .toUpperCase()) are valid.
let greeting: string = 'Hello';
console.log(greeting.toUpperCase()); // 'HELLO'
console.log(greeting.length);        // 5

The number Type

TypeScript has one number type for both integers and floats, matching JavaScript. There is no separate int or float type.
let age: number = 25;
let pi: number = 3.14159;
let hex: number = 0xFF;

The boolean Type

boolean represents true or false. TypeScript prevents truthy/falsy coercion bugs by enforcing that boolean variables hold only true or false.
let isDone: boolean = false;
let hasAccess: boolean = true;
// isDone = 1; // Error — not a boolean

Template Literals Work with string

TypeScript types template literal expressions. Embedding a non-string in a template literal is allowed because JavaScript coerces it to a string.
let name: string = 'World';
let n: number = 42;
let msg = `Hello ${name}, you scored ${n}`;

Primitive vs Wrapper Objects

Always use lowercase string, number, boolean — not String, Number, Boolean. The uppercase versions are object wrapper types and should rarely be used.
// Correct
let a: string = 'hello';

// Avoid
let b: String = new String('hello'); // wrapper object

Type Annotations on Function Parameters

Annotating function parameters with primitives catches wrong-argument bugs at compile time.
function add(a: number, b: number): number {
  return a + b;
}
add(2, 3);      // OK
// add(2, '3'); // Error

When to Skip Annotations

If you initialize a variable with a value, TypeScript infers the type. Adding an explicit annotation is optional but can improve readability in function signatures.
// Both are fine:
const x = 5;          // inferred number
const y: number = 5;  // explicit number

Quick Check

What is the correct TypeScript primitive type for the value 3.14?

Recap

Use string, number, and boolean for TypeScript's primitive types. TypeScript infers types from initial values, but explicit annotations are useful in function signatures. Always use lowercase type names.

Frequently asked questions

Is the “Primitive Types: string, number, boolean” lesson free?

Yes — the full text of “Primitive Types: string, number, boolean” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Primitive Types: string, number, boolean”?

Annotate variables with TypeScript's three core primitives. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Primitive Types: string, number, boolean” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Primitive Types: string, number, boolean
  2. The any Type and Why to Avoid It
  3. unknown vs any: The Safer Choice
  4. null, undefined, and Strict Null Checks
← Back to TypeScript Academy