0
0
Typescriptprogramming~10 mins

What types exist only at compile time in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - What types exist only at compile time
Write TypeScript code with types
Compiler checks types
Types removed during compilation
Output JavaScript without types
Run JavaScript code at runtime
TypeScript types exist only during compilation to check code correctness and are removed before running JavaScript.
Execution Sample
Typescript
type User = { name: string; age: number };

function greet(user: User) {
  return `Hello, ${user.name}`;
}

const person = { name: "Alice", age: 30 };
greet(person);
Defines a type for a user object and uses it to check the function argument at compile time.
Execution Table
StepActionType CheckResult
1Define type UserUser type createdNo runtime code generated
2Define function greet with User parameterParameter checked against User typeFunction code generated (types erased)
3Create object person with name and ageObject matches User typeObject created at runtime
4Call greet(person)Argument matches User typeFunction runs, returns greeting string
5Compile to JavaScriptTypes removedOutput JS has no types
6Run JavaScript codeNo type checksOutputs: Hello, Alice
💡 Compilation ends with types removed; runtime runs plain JavaScript without types
Variable Tracker
VariableStartAfter Step 3After Step 4Final
UserType definedType exists only at compile timeRemoved after compilationNo runtime presence
personUndefined{ name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }Same object at runtime
greetFunction defined with typeFunction existsReturns 'Hello, Alice'Function runs at runtime
Key Moments - 3 Insights
Why don't types appear in the final JavaScript code?
Types are only for compile-time checks and are removed during compilation, as shown in execution_table step 5.
Can types affect runtime behavior?
No, types do not exist at runtime and cannot change how the JavaScript runs, confirmed by step 6 in execution_table.
What happens if an object doesn't match the type?
The compiler shows an error before generating JavaScript, preventing runtime errors, as implied in step 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what happens to the types?
AThey are converted to runtime checks
BThey are removed from the output JavaScript
CThey become variables in JavaScript
DThey are ignored but stay in the code
💡 Hint
Check the 'Result' column in step 5 of execution_table
According to variable_tracker, what is the state of 'User' after compilation?
AIt is removed and does not exist at runtime
BIt becomes a JavaScript function
CIt exists as a runtime object
DIt is converted to a string
💡 Hint
Look at the 'User' row in variable_tracker under 'Final'
If the object 'person' did not match the 'User' type, what would happen during compilation?
AThe types would be ignored and code runs
BThe code would run but with warnings
CThe compiler would show an error and stop
DThe runtime would throw an error
💡 Hint
Refer to key_moments about type mismatch and execution_table steps 2 and 3
Concept Snapshot
TypeScript types exist only at compile time.
They help check code correctness before running.
Types are removed when compiling to JavaScript.
No types or checks remain at runtime.
Errors happen during compilation if types don't match.
Full Transcript
In TypeScript, types like 'User' exist only during compilation to check code correctness. When you write a function with typed parameters, the compiler verifies the arguments match the types. These types do not produce any JavaScript code and are removed during compilation. The final JavaScript runs without any type information or checks. If an object does not match the expected type, the compiler shows an error and stops before generating JavaScript. This ensures safer code without affecting runtime performance.