Skip to content
Discuss a project···
josip
All articlesJun 1, 20214 min readprogramming
Field notes

Learning TypeScript Before JavaScript

Learning TypeScript Before JavaScript

JavaScript is the usual starting point for web development. I think a beginner can start with TypeScript instead, provided the course or project still teaches the JavaScript runtime underneath it.

TypeScript adds a static analysis layer. It catches some mistakes before execution and gives editors more information for navigation, completion, and refactoring. Those benefits are useful from the first small program.

Basic type errors become visible

Type annotations state what a variable or function accepts:

let isDone: boolean = false;
let decimal: number = 6;
let color: string = "BlanchedAlmond";

The compiler can reject an accidental string assignment to decimal without waiting for the affected code path to run.

Function signatures are even more useful because they describe the contract at the call site:

function calculateTotal(quantity: number, price: number): number {
  return quantity * price;
}

An editor can show that contract during completion and rename references across a project with much more confidence than it can in untyped JavaScript.

Interfaces and type aliases

Interfaces and type aliases can both describe object shapes. Interfaces support declaration merging and are commonly used for object-oriented APIs. Type aliases can also represent primitives, unions, intersections, mapped types, and conditional types.

A beginner does not need a complete decision table. Pick the convention used by the project and learn the distinctions when a concrete use case appears.

interface Person {
  name: string;
  age: number;
}
 
const john: Person = {
  name: "John",
  age: 30,
};
 
interface StringArray {
  [index: number]: string;
}
 
const fruits: StringArray = ["Apple", "Banana", "Mango"];
 
type Dictionary = {
  [index: string]: unknown;
};
 
const person: Dictionary = {
  name: "John",
  age: 30,
  address: "123 Main St",
};

The important lesson is structural typing. A value satisfies the contract when its shape is compatible, even if it was not created from a named class.

Generics preserve relationships between types

A generic type captures a relationship without replacing everything with any. In this example, the response body and return type stay connected:

interface ApiResponse<T> {
  status: number;
  type: string;
  data: T;
}
 
function handleResponse<T>(response: ApiResponse<T>): T {
  if (response.status !== 200) {
    throw new Error(`Request failed with status code ${response.status}`);
  }
 
  return response.data;
}
 
declare const userResponse: ApiResponse<User>;
const user = handleResponse(userResponse);

Because userResponse contains User data, handleResponse returns a User. The compiler retains that information through the function instead of widening the result.

Modern syntax and tooling

TypeScript understands current JavaScript syntax and can target older runtime versions when a project requires it. Its language server provides completion, reference search, inline documentation, and refactoring based on the inferred type graph.

These tools shorten the feedback loop, especially in a codebase with many modules. They do not prove that a program is correct. Network responses, user input, database contents, and other runtime values still need validation.

The JavaScript underneath still matters

Starting with TypeScript has a risk: the type system can hide gaps in a beginner's understanding of JavaScript. Types disappear during compilation. The runtime still has coercion, prototypes, closures, an event loop, promises, modules, and platform-specific APIs.

A TypeScript-first path should therefore include plain JavaScript exercises and regular inspection of emitted code. Learn which checks exist only in the editor and which guarantees survive at runtime.

A retro mountain bike on a trail

I compare the transition to learning a motorcycle before a bicycle. Some controls and instincts transfer, but the simpler machine exposes balance more directly. Moving from TypeScript to JavaScript can feel similarly bare because annotations and editor guidance disappear.

A reasonable learning order

Start with TypeScript syntax, basic values, functions, objects, arrays, and unions. Run the resulting JavaScript and use browser or Node.js debugging tools from the beginning. Add interfaces and generics when the program contains a relationship they can express.

This route gives a beginner early compiler feedback without pretending that TypeScript replaces JavaScript. The strongest outcome is fluency in both: TypeScript for static analysis and JavaScript for the runtime that executes the program.

Found this useful? Share on X or send a note.Josip
Read next
More notes.
All articles