TypeScript

TypeScript Data Types vs JavaScript Data Types: Complete Comparison

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.

TypeJavaScript ExampleTypeScript Example
stringlet city = "Paris";let city: string = "Paris";
numberlet age = 25;let age: number = 25;
booleanlet isAdmin = true;let isAdmin: boolean = true;
nulllet n = null;let n: null = null;
undefinedlet u;let u: undefined = undefined;
bigintlet big = 123n;let big: bigint = 123n;
symbollet 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 type or interface.
// 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:

TypeDescriptionExample
anyDisables type checking (not recommended).let data: any = 10; data = "string";
unknownSafer alternative to any, requires type checks.let input: unknown; if (typeof input === "string") { console.log(input.toUpperCase()); }
neverRepresents values that never occur (e.g., errors).function fail(): never { throw new Error("Error!"); }
voidUsed for functions that don’t return anything.function log(msg: string): void { console.log(msg); }

Summary Table: TypeScript vs JavaScript Data Types

CategoryJavaScriptTypeScript
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.


Related posts
TypeScript

Literals in TypeScript: String, Number, Boolean, Template, and Compound Literals

Literals are one of the fundamental building blocks of TypeScript programming. They represent fixed…
Read more
TypeScript

Utility Types in TypeScript: Pick, Omit, Partial, Required, Readonly, Record...

✅ — Utility Types are one of the most powerful features of TypeScript, but also one of the most…
Read more
TypeScript

Complete TypeScript Tutorial Online: Master TypeScript in 2025

TypeScript has revolutionized modern web development by bringing static typing to JavaScript, making…
Read more