Core Primitives
The foundational types of JavaScript, statically enforced by TypeScript.
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.
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.
name is locked to stringage is locked to numberisActive is locked to booleanCheck yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- TypeScript's core primitives mirror JavaScript's runtime primitives:
string,number,boolean,symbol, andbigint. - Avoid redundant annotations: if you assign a value immediately, let TypeScript infer the type.
- Never use uppercase object wrappers (
String,Number) for typing.
Done with this concept?
Mark it complete to track your progress. No login needed.