A Type System is a set of rules that determines what kind of values can be assigned to variables, passed to functions, and returned from functions.
For example, JavaScript allows:
But TypeScript allows us to define the expected type:
What is Type Annotation?
Type Annotation means explicitly specifying the type of a variable, function parameter, or return value.
Syntax:
Examples:
Here:
namemust contain a string.agemust contain a number.isLoginmust contain a boolean.
Type Inference
TypeScript can automatically understand the type from the assigned value.
TypeScript automatically infers:
You don't always need to write types manually.
Difference:
Type Annotation | Type Inference |
|---|---|
You specify the type | TypeScript detects the type |
|
|
Explicit | Automatic |
2. String
The string type is used to store textual data.
Examples include:
- Student names
- Email addresses
- Phone numbers stored as text
- Addresses
- Passwords
Syntax
You can use three types of string notation.
A. Double quotes
B. Single quotes
C. Template literals
Template literals are especially useful when combining variables with strings.
3. Number
Unlike some other programming languages, TypeScript doesn't have separate int, float, and double types.
Special number values
JavaScript also supports some special numerical values.
BigInt
The bigint type is used to store very large integer values that exceed the safe integer range of JavaScript's number type.
First, understand the limitation of number.
JavaScript's maximum safe integer is:
Output:
If you need to work with integers larger than this safely, use bigint.
Syntax
Notice the n at the end.
Without n, JavaScript treats the value as a number.
Important differences between Number and BigInt
You cannot directly perform arithmetic between number and bigint.
You must convert them first, provided the conversion is safe and appropriate.
Important points:
- BigInt only supports integers, not decimal values.
- BigInt cannot be directly mixed with Number in arithmetic.
- JSON.stringify does not serialize BigInt by default.
Practical use: Financial calculations involving very large integer quantities, cryptography, and high-precision integer operations.
Symbol
symbol is a primitive data type in JavaScript and TypeScript.
It creates a unique identifier.
Even if two symbols have the same description, they are different values.
Every Symbol is unique
Consider:
Practical example: Object properties
Suppose you have an object:
Symbol.for()
JavaScript also provides Symbol.for().
Unlike Symbol(), it uses a global symbol registry.
Null
null represents the intentional absence of a value.
In simple words, you know a variable exists, but currently it has no value.
Here, the variable can contain only null.
Practical example
Suppose a student hasn't provided their profile picture.
Important: Union Type
Notice this:
The | symbol means OR.
It means username can contain either:
- A string
- null
Both are valid:
8. Undefined
undefined represents a variable or property that has not been assigned a value.
Syntax
Any
Now let's understand any.
any disables TypeScript's normal type checking for a particular value.
It allows you to assign almost any type of value to a variable.
Syntax
Now you can assign different types:
A. Why do we need any?
Suppose you receive data from an external API.
You don't know its structure yet.
B. Problem with any
Consider:
Why?
Because toFixed() is a number method, but the actual value is a string.
TypeScript doesn't warn you because you used any.
Unknown
unknown is similar to any, but it is safer.
It allows you to store values of any type, but you cannot directly use that value without checking its type.
Syntax
You can assign different values:
All are allowed.
Why is unknown safer than any?
Consider any:
TypeScript doesn't report an error.
Now consider unknown:
TypeScript prevents this because it doesn't know whether value is a number.
B. How to use unknown?
First, check the type.
This process is called Type Narrowing.
TypeScript narrows the possible type after checking it.
Practical example: API response
Suppose your Express API receives data from an external source.
You should not blindly trust its structure.
Any vs Unknown
Feature | Any | Unknown |
|---|---|---|
Accepts any value | Yes | Yes |
Can assign any value | Yes | Yes |
Direct property access | Allowed | Not allowed |
Direct method calls | Allowed | Not allowed |
Requires type checking | No | Yes |
Safety | Less safe | More safe |
Prefer unknown over any when you don't know the incoming data type.
Void
void is mainly used as a function return type.
It indicates that a function does not return a meaningful value.
A. Function without return value
C. Can void return undefined?
Yes.
This is valid.
But returning a number is not:
Never
never represents a value that can never occur.
It is used when a function never completes normally or when TypeScript determines that no possible value remains.
There are two important use cases.
A. Function that never returns
Consider a function that always throws an error.
This function never completes normally.
It throws an error instead of returning a value.
Important concept: strictNullChecks
One more important concept before completing this module.
In your tsconfig.json:
When strict is enabled, strictNullChecks is also enabled.
Consider:
But:
This helps prevent many common programming errors.