TypeScript helps catch errors early by adding types to JavaScript. Migrating makes your code safer and easier to understand.
Migrating JavaScript to TypeScript
Introduction
Syntax
Typescript
Change file extension from .js to .ts Add type annotations like: function greet(name: string): string { return `Hello, ${name}!`; }
TypeScript files use the .ts extension instead of .js.
Adding types like string or number helps the compiler check your code.
Examples
Typescript
function add(a: number, b: number): number { return a + b; }
message must always be a string.Typescript
let message: string = 'Hello!';
Typescript
const numbers: number[] = [1, 2, 3];
Sample Program
This program greets a user by name. TypeScript checks that name is a string.
Typescript
function greet(name: string): string { return `Hello, ${name}!`; } const userName = 'Alice'; console.log(greet(userName));
Important Notes
Start by renaming your .js files to .ts.
Add types gradually; you don't need to type everything at once.
Use any type temporarily if you are unsure, but try to replace it later.
Summary
TypeScript adds types to JavaScript for safer code.
Rename files to .ts and add type annotations step-by-step.
This helps catch errors early and improves code clarity.
Practice
1. What is the main benefit of migrating JavaScript code to TypeScript?
easy
Solution
Step 1: Understand TypeScript's purpose
TypeScript adds static types to JavaScript, helping find errors before running code.Step 2: Compare options
Only It adds type safety to catch errors early correctly states the benefit of type safety. Other options are incorrect or unrelated.Final Answer:
It adds type safety to catch errors early -> Option AQuick Check:
Type safety = D [OK]
Hint: TypeScript adds types to catch errors early [OK]
Common Mistakes:
- Thinking TypeScript makes code run faster
- Believing TypeScript removes JavaScript features
- Confusing TypeScript with other languages
2. Which of the following is the correct way to declare a typed variable in TypeScript?
easy
Solution
Step 1: Recall TypeScript variable syntax
TypeScript useslet variableName: type = value;to declare typed variables.Step 2: Check each option
Only let age: number = 30; follows the correct syntax. Others have misplaced type or incorrect order.Final Answer:
let age: number = 30; -> Option BQuick Check:
Correct typed variable syntax = B [OK]
Hint: Use colon after variable name for type [OK]
Common Mistakes:
- Placing type after value
- Omitting colon between name and type
- Using wrong order of type and value
3. What will be the output of this TypeScript code after migration?
function greet(name: string) {
return `Hello, ${name.toUpperCase()}!`;
}
console.log(greet("world"));medium
Solution
Step 1: Analyze the function behavior
The function takes a string and returns a greeting with the name in uppercase.Step 2: Evaluate the output of greet("world")
Callinggreet("world")returns "Hello, WORLD!" becausetoUpperCase()converts to uppercase.Final Answer:
"Hello, WORLD!" -> Option CQuick Check:
String uppercase greeting = C [OK]
Hint: Check string methods and template literals output [OK]
Common Mistakes:
- Ignoring toUpperCase effect
- Expecting original case output
- Confusing runtime and compile errors
4. Identify the error in this TypeScript migration:
function add(a, b: number): number {
return a + b;
}medium
Solution
Step 1: Check parameter types
In TypeScript, all parameters should have type annotations for clarity and safety.Step 2: Identify missing type
Parameter 'a' lacks a type, causing a compile error. Other parts are correct.Final Answer:
Parameter 'a' is missing a type annotation -> Option AQuick Check:
All parameters need types = A [OK]
Hint: All function parameters need types in TypeScript [OK]
Common Mistakes:
- Leaving some parameters untyped
- Misunderstanding return type syntax
- Thinking function names have restrictions
5. You have this JavaScript object:
const user = { id: 1, name: "Alice", active: true };
How would you migrate it to TypeScript with a proper type annotation?hard
Solution
Step 1: Define precise object type
Use an inline type annotation with exact property types for clarity and safety.Step 2: Compare options
const user: { id: number; name: string; active: boolean } = { id: 1, name: "Alice", active: true }; correctly annotates the object with property types. const user = { id: number, name: string, active: boolean } = { id: 1, name: "Alice", active: true }; is invalid syntax. const user: object = { id: 1, name: "Alice", active: true }; is too generic. const user: any = { id: 1, name: "Alice", active: true }; disables type checking.Final Answer:
const user: { id: number; name: string; active: boolean } = { id: 1, name: "Alice", active: true }; -> Option DQuick Check:
Precise object type annotation = A [OK]
Hint: Use inline type with property names and types [OK]
Common Mistakes:
- Using 'any' which disables type safety
- Incorrect syntax for object type annotation
- Using generic 'object' type losing property info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
