TypeScript Primitive Types
TypeScript has three main primitive types that form the foundation
of type-safe programming: string, number, and
boolean. Unlike Java with its 8 primitive types, TypeScript keeps
it simple while maintaining powerful type safety. Let's master these fundamental
types!
The 3 Primitive Types
TypeScript's primitive types correspond directly to JavaScript's primitive values, but with added type safety:
| Type | Description | Example Values |
|---|---|---|
string |
Text data | "hello", 'world', `template`
|
number |
All numeric values (integers and decimals) | 42, 3.14, -10,
0xFF
|
boolean |
True or false values | true, false |
int, long,
float, or double - just number!
String Type
The string type represents textual data. TypeScript supports three
ways to define strings:
Click Run to execute your code
Template Literals (Backticks)
Template literals are the most powerful string type. They support:
- String interpolation: Embed expressions with
${} - Multi-line strings: No need for escape characters
- Expression evaluation: Calculate values inside strings
Click Run to execute your code
Number Type
The number type handles all numeric values - integers, decimals,
hexadecimal, binary, and octal:
Click Run to execute your code
Special Number Values
TypeScript's number type includes special values:
| Value | Description | Example |
|---|---|---|
Infinity |
Positive infinity | 1 / 0 → Infinity |
-Infinity |
Negative infinity | -1 / 0 → -Infinity |
NaN |
Not a Number | "hello" * 2 → NaN |
console.log(0.1 + 0.2); // 0.30000000000000004 (not exactly 0.3!)
For financial calculations, consider using libraries like
decimal.js or big.js.
Boolean Type
The boolean type has only two values: true and
false. It's essential for conditional logic:
Click Run to execute your code
boolean type only
accepts literal true or false:
let isActive: boolean = true; // ✓ Correct
let isActive: boolean = 1; // ✗ Error: Type 'number' is not assignable to type 'boolean'
Type Annotations vs Type Inference
TypeScript can automatically infer types, but you can also explicitly annotate them:
Click Run to execute your code
When to Use Annotations
| Scenario | Recommendation |
|---|---|
| Variable initialized with a value | Let TypeScript infer (no annotation needed) |
| Variable declared without initialization | Use explicit annotation |
| Function parameters | Always use annotations |
| Function return types | Recommended for clarity |
Type Safety in Action
TypeScript prevents common type-related errors at compile time:
Common Type Errors
// Error: Type 'number' is not assignable to type 'string'
let userName: string = 42;
// Error: Type 'string' is not assignable to type 'number'
let age: number = "25";
// Error: Type 'string' is not assignable to type 'boolean'
let isActive: boolean = "true";
// Correct usage
let userName: string = "Alice";
let age: number = 25;
let isActive: boolean = true;
Exercise: User Profile
Task: Create a user profile with proper type annotations!
Requirements:
- Create variables for: firstName, lastName, age, email, isVerified, balance
- Use appropriate primitive types for each
- Create a greeting message using template literals
- Display all user information
Click Run to execute your code
💡 Hint
Remember: names and email are strings, age and balance are numbers, and isVerified is a boolean. Use template literals to create a nice greeting!
Summary
- string - Text data, supports single quotes, double quotes, and template literals
- number - All numeric values (integers, decimals, hex, binary, octal)
- boolean - Only
trueorfalse - Template literals (
`backticks`) support string interpolation with${} - TypeScript can infer types, but explicit annotations improve clarity
- Type annotations prevent runtime errors by catching type mismatches at compile time
What's Next?
Now that you understand primitive types, let's explore more complex data structures! In the next lesson, we'll learn about Arrays & Tuples - how to store collections of values with type safety.
Enjoying these tutorials?