# Core Primitives

> The foundational types of JavaScript, statically enforced by TypeScript.

Subject: TypeScript | Lesson 1 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/primitive-types
Next: [any vs unknown](https://codewithanimation.in/typescript/any-vs-unknown.md)

## What are Primitive Types?

JavaScript has a handful of foundational, atomic data types known as primitives. The most common are strings, numbers, and booleans. Modern JavaScript also includes symbols and bigints.

TypeScript wraps these exact same runtime primitives in a static type system. When you declare a variable as a `string`, the compiler strictly forbids you from later assigning a number or a boolean to it.

## Why enforce them?

Dynamic typing is a double-edged sword. In raw JavaScript, `let age = 25` can easily become `age = "twenty-five"` later in the execution flow. This flexibility is the root cause of countless runtime crashes and hidden bugs.

By strictly enforcing primitive boundaries, TypeScript catches these type-coercion errors the moment you type them in your editor, long before the code ever reaches a browser.

## How to use them

You define a primitive type using a postfix annotation: a colon followed by the type name (always lowercase).

For example, `let name: string = "Alice";` explicitly tells the compiler that this variable can only ever hold text.

However, TypeScript is usually smart enough to figure this out on its own. If you write `let name = "Alice";`, TypeScript instantly infers that `name` is a `string` without you having to explicitly type it.

## Common mistake: The over-typing trap

A common mistake for beginners coming from heavily typed languages like Java or C# is explicitly typing every single variable. Writing `let score: number = 100;` is redundant.

TypeScript's inference engine is incredibly powerful. Let it do the heavy lifting, and reserve explicit annotations for when the compiler cannot guess your intent (like initializing an empty variable to be assigned later).

## Tip: Always lowercase

Always use the lowercase type names (`string`, `number`, `boolean`). The uppercase versions (`String`, `Number`, `Boolean`) refer to special built-in JavaScript object wrappers, which you almost never want to use.

## Try it: Try it

Watch the compiler lock in the types. We use explicit annotations on the left, but notice how TypeScript would have inferred the exact same types automatically.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/primitive-types

## Check yourself

### Question 1

Which of the following is the correct way to type a boolean variable in TypeScript?

- A. let isActive: Boolean = true;
- B. let isActive: bool = true;
- C. let isActive: boolean = true;

Correct answer: C

TypeScript uses lowercase primitive names exactly as they are evaluated by JavaScript's `typeof` operator: `string`, `number`, and `boolean`.

- Why not A: Uppercase 'Boolean' refers to the JS object wrapper, not the primitive type.
- Why not B: 'bool' is not a valid TypeScript type; it is used in C/C++.

## Remember this

- TypeScript's core primitives mirror JavaScript's runtime primitives: `string`, `number`, `boolean`, `symbol`, and `bigint`.
- Avoid redundant annotations: if you assign a value immediately, let TypeScript infer the type.
- Never use uppercase object wrappers (`String`, `Number`) for typing.
