0
0
Typescriptprogramming~10 mins

Migrating JavaScript to TypeScript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Migrating JavaScript to TypeScript
Start with JS code
Add TypeScript types
Fix type errors
Compile TS to JS
Run compiled JS
Iterate and improve types
End with safer code
This flow shows how you start with JavaScript, add types, fix errors, compile, run, and improve your code step-by-step.
Execution Sample
Typescript
function greet(name) {
  return "Hello, " + name;
}

console.log(greet(42));
This JavaScript function greets a name but is called with a number, which TypeScript will catch as an error.
Execution Table
StepCode LineActionTypeScript CheckResult/Output
1function greet(name) {Define function greet with parameter namename has implicit any typeNo error
2 return "Hello, " + name;Return greeting string concatenated with nameNo errorNo output yet
3}End functionNo errorNo output
4console.log(greet(42));Call greet with number 42No error (if no types), Error if typedIf no types, outputs 'Hello, 42'
5Add type: function greet(name: string) {Add type annotation to parameterTypeScript error appears on call with 42No output
6Fix call: console.log(greet("Alice"));Call greet with string 'Alice'No errorOutput: Hello, Alice
7Compile TS to JSTypeScript compiles to JavaScriptNo errorJS code ready to run
8Run compiled JSRun output in JS environmentNo errorConsole prints: Hello, Alice
💡 Execution stops after running compiled JavaScript with correct types and no errors.
Variable Tracker
VariableStartAfter Step 4After Step 6Final
nameundefined42 (incorrect type)"Alice" (correct type)"Alice"
Key Moments - 3 Insights
Why does TypeScript show an error when calling greet(42)?
Because after adding the type annotation 'name: string', passing a number (42) does not match the expected string type, as shown in execution_table step 5.
What happens if you don't add types to the function parameter?
TypeScript treats the parameter as 'any' type, so no error occurs even if you pass a number, but you lose type safety. This is shown in execution_table step 4 where no error appears without types.
How does adding types improve the code?
Adding types helps catch mistakes early, like passing wrong argument types, making the code safer and easier to maintain, as seen in execution_table steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what type error appears at step 4?
AMissing return statement
BPassing a number to a string parameter
CUsing an undefined variable
DSyntax error in function definition
💡 Hint
Check the 'TypeScript Check' column at step 4 in the execution_table.
At which step does the function call get fixed to avoid the type error?
AStep 3
BStep 2
CStep 6
DStep 8
💡 Hint
Look for the step where greet is called with a string argument in the execution_table.
If you remove the type annotation from the parameter, what changes in the execution?
ANo type error will appear when calling greet(42)
BTypeScript will still show an error on greet(42)
CThe function will not compile
DThe output will be empty
💡 Hint
Refer to the 'TypeScript Check' column in step 4 before adding types.
Concept Snapshot
Migrating JavaScript to TypeScript:
- Start with JS code
- Add type annotations (e.g., function greet(name: string))
- Fix type errors shown by TypeScript
- Compile TS to JS
- Run compiled JS
- Iteratively improve types for safer code
Full Transcript
This visual execution shows how to migrate JavaScript code to TypeScript by adding type annotations, fixing errors, compiling, and running the code. Initially, a JavaScript function greet accepts any parameter. When called with a number, it works but is unsafe. Adding a type annotation 'name: string' causes TypeScript to show an error if a number is passed. Fixing the call to pass a string removes the error. Compiling TypeScript produces JavaScript that runs safely. Variables and errors are tracked step-by-step to help beginners understand the migration process.