When transitioning from JavaScript to TypeScript, one of the most important differences lies in how the two languages handle data types. JavaScript is dynamically typed, while TypeScript adds static typing on top of it. This means developers can define types explicitly, catch errors at compile time, and write more predictable and maintainable code.
In this article, we will compare JavaScript and TypeScript data types, including primitives, composite types, generics, literal types, union & intersection types, and special types.
1. Primitive Data Types
Both JavaScript and TypeScript share the same set of primitive values.
However, TypeScript allows you to explicitly annotate them.
| Type | JavaScript Example | TypeScript Example |
|---|---|---|
| string | let city = "Paris"; | let city: string = "Paris"; |
| number | let age = 25; | let age: number = 25; |
| boolean | let isAdmin = true; | let isAdmin: boolean = true; |
| null | let n = null; | let n: null = null; |
| undefined | let u; | let u: undefined = undefined; |
| bigint | let big = 123n; | let big: bigint = 123n; |
| symbol | let id = Symbol("id"); | let id: symbol = Symbol("id"); |
👉 In JavaScript, the types are inferred at runtime. In TypeScript, they are checked at compile time, preventing type-related bugs.
2. Composite Types: Objects, Arrays, Tuples
Objects
- JavaScript: No type restrictions.
- TypeScript: You can define object shapes with
typeorinterface.
// TS object type
type User = {
name: string;
age: number;
};
let person: User = { name: "Alice", age: 30 };
Arrays
- JavaScript: Flexible arrays, any value can be inserted.
- TypeScript: Arrays can be typed for consistency.
let numbers: number[] = [1, 2, 3];
let mixed: (string | number)[] = ["Alice", 25];
Tuples
- Not available in JavaScript directly.
- TypeScript introduces tuples for fixed-length, ordered arrays.
let userTuple: [string, number] = ["Bob", 40];
3. Generics in TypeScript
JavaScript does not have generics.
TypeScript introduces generics to create reusable, type-safe components.
// Generic function
function identity<T>(value: T): T {
return value;
}
let num = identity<number>(42); // T = number
let str = identity("Hello"); // T = string (inferred)
Generics are widely used in collections, utility types, and libraries (e.g., React hooks like useState<T>).
4. Literal Types
- JavaScript: Literals exist only as values.
- TypeScript: Literal types allow exact values as types.
let direction: "left" | "right" | "up" | "down";
direction = "left"; // ✅
direction = "forward"; // ❌ Error
This is extremely useful for enums, configuration, and reducing runtime errors.
5. Union and Intersection Types
Union Types
- JavaScript: No explicit unions, you’d check types manually at runtime.
- TypeScript: Use
|to allow multiple types.
let value: string | number;
value = "hello";
value = 42;
Intersection Types
- JavaScript: Not available.
- TypeScript: Use
&to combine multiple types into one.
type Person = { name: string };
type Worker = { company: string };
type Employee = Person & Worker;
let e: Employee = { name: "Alice", company: "TechCorp" };
6. Special Types in TypeScript
TypeScript extends JavaScript with special types:
| Type | Description | Example |
|---|---|---|
| any | Disables type checking (not recommended). | let data: any = 10; data = "string"; |
| unknown | Safer alternative to any, requires type checks. | let input: unknown; if (typeof input === "string") { console.log(input.toUpperCase()); } |
| never | Represents values that never occur (e.g., errors). | function fail(): never { throw new Error("Error!"); } |
| void | Used for functions that don’t return anything. | function log(msg: string): void { console.log(msg); } |
Summary Table: TypeScript vs JavaScript Data Types
| Category | JavaScript | TypeScript |
|---|---|---|
| Primitives | ✅ Yes | ✅ Yes (with annotations) |
| Objects & Arrays | ✅ Flexible | ✅ Strongly typed |
| Tuples | ❌ No | ✅ Yes |
| Generics | ❌ No | ✅ Yes |
| Literal Types | ❌ No | ✅ Yes |
| Union & Intersection | ❌ No | ✅ Yes |
Special Types (any, unknown, never, void) | ❌ No | ✅ Yes |
Conclusion
- JavaScript provides dynamic typing, which is flexible but error-prone.
- TypeScript adds static typing and powerful features such as tuples, generics, unions, intersections, and literal types, making code safer and more maintainable.
For large-scale applications, TypeScript’s type system significantly improves developer productivity, reduces bugs, and makes code easier to refactor.