TypeScript for JavaScript Developers
Bring JavaScript experience to typed contracts, narrowing, functions, collections and class modules. Build and test a validated order-processing tool in TypeScript.
JavaScript and TypeScript side by side
The same program in JavaScript and in TypeScript, from the lessons of this course.
Run TypeScript
TypeScript adds a checker to JavaScript. Types disappear before execution; strings, numbers and template literals still behave as JavaScript. The editor runs the TypeScript target. The JavaScript examples are comparison programs. Run the supplied two-file…
JavaScript1const name = "Ada";2console.log(`Hello, ${name}!`);TypeScript1const name: string = "Ada";2console.log(`Hello, ${name}!`);Annotations and Inference
Keep JavaScript arithmetic and add annotations at boundaries. A variable initialized to a number usually needs no annotation because inference already knows its type. A parameter needs a declared contract here. TypeScript number is still the JavaScript…
JavaScript1const price = 2.5;2const count = 4;3console.log(price * count);TypeScript1const price: number = 2.5;2const count = 4;3console.log(price * count);Narrowing with typeof
TypeScript follows branches and narrows a union inside each branch. The guard itself is ordinary JavaScript. Double numeric inputs; for strings return length. For this exercise strings contain ASCII characters, so string length also equals the character…
JavaScript1function measure(value) { return typeof value === "number" ? value * 2 : value.length; }2console.log(measure("abc"));TypeScript1function measure(value: number | string): number { return typeof value === "number" ? value * 2 : value.length; }2console.log(measure("abc"));Function Parameters
Annotate parameter and return boundaries while preserving JavaScript defaults and rest arguments. A default is used for undefined or an omitted argument, not for zero or null. Sum all values and multiply by factor, defaulting to two when factor is absent.…
JavaScript1function total(factor = 2, ...values) { return values.reduce((s, n) => s + n, 0) * factor; }2console.log(total(undefined, 2, 3));TypeScript1function total(factor: number = 2, ...values: number[]): number { return values.reduce((s, n) => s + n, 0) * factor; }2console.log(total(undefined, 2, 3));Readonly and Copies
Readonly prevents mutation through a typed reference; it does not freeze the JavaScript object. Sorting mutates an array, so copy before sorting. Return ascending numeric values without changing input. JavaScript default sort compares string forms; use a…
JavaScript1const values = [10, 2, 1];2console.log(JSON.stringify([...values].sort((a, b) => a - b)));TypeScript1const values: readonly number[] = [10, 2, 1];2console.log(JSON.stringify([...values].sort((a, b) => a - b)));Errors and Runtime Checks
JSON.parse performs parsing, not schema validation. Assign its result to unknown and check the runtime category before doing arithmetic. Here input is a string containing another JSON document: parse it and double finite numbers; return invalid for malformed…
JavaScript1try { const value = JSON.parse("{"); console.log(value); } catch (error) { console.log(error instanceof SyntaxError ? "invalid" : "unexpected"); }TypeScript1try { const value: unknown = JSON.parse("{"); console.log(value); } catch (error: unknown) { console.log(error instanceof SyntaxError ? "invalid" : "unexpected"); }
Syllabus
8 chapters32 lessons1 project32 challenges60 quiz questionsStart Here
1 lessonTyped Contracts
5 lessonsTyped Flow
5 lessonsTyped Functions
5 lessonsTyped Collections
5 lessonsReliable Modules and Objects
5 lessonsProject: Typed Orders
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is TypeScript for JavaScript Developers for?
For people who already write JavaScript and want to learn TypeScript. Each lesson starts from JavaScript you know and shows how TypeScript does the same thing, so you skip the beginner basics.
Do I need to know TypeScript already?
No. The course starts with your first TypeScript program. You need JavaScript basics: variables, conditions, loops, functions and collections.
How many lessons does TypeScript for JavaScript Developers have?
TypeScript for JavaScript Developers contains 32 interactive lessons you can complete at your own pace.
What does TypeScript for JavaScript Developers cover?
The course is organized into chapters covering: Start Here, Typed Contracts, Typed Flow, Typed Functions, and more.
Is TypeScript for JavaScript Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.