What is "type safety"?
1. What "type safety" means
Type safety is a property of a language that guarantees operations on data are performed only with correct types, preventing errors related to the incorrect use of values.
In simpler terms:
TypeScript makes sure you don't do things with data that you shouldn't
- for example, calling a string method on a number, or adding an object to an array, and so on.
Example without type safety (JavaScript)
const user = "Tim";
console.log(user.toFixed(2)); // Error at runtimeJavaScript does not know that
useris a string. The error happens at runtime, when the program is already running.
Example with type safety (TypeScript)
const user: string = "Tim";
console.log(user.toFixed(2)); // Error at compile timeTypeScript will not let the program run - it understands that
toFixed()exists only on a number, not on a string.
The error is caught before the code runs - the program is safer.
2. What type safety is about
Type-safe code guarantees:
- That a value matches its type.
→ if
user: User, then it hasname,email, and so on. - That operations on a value are valid for that type.
→ you cannot call
.map()on a number. - That function results are predictable.
→ if a function returns
Promise<string>, it will not returnnumber.
3. Type safety in action
interface Product {
id: number;
title: string;
price: number;
}
function printPrice(p: Product) {
console.log(p.price.toFixed(2));
}
printPrice({ id: 1, title: "Shirt", price: 49.9 }); // correct
printPrice({ id: 2, title: "Shoes", price: "49.9" }); // Type errorThe second call will not compile, because
priceis a string, not a number.This is what type safety is: protection from errors caused by incorrect data types.
4. What makes code "type unsafe"
TypeScript becomes less type-safe if you use "holes" in the type system:
let data: any = 5;
data = "Hello";
data = { x: true };
console.log(data.toFixed(2)); // Error at runtimeThe
anytype turns off the safety system - the compiler stops checking types.That is why
anyis considered the enemy of type safety.
5. How TypeScript achieves type safety
TypeScript achieves type safety through:
- strict static typing (checking types before running);
- strict mode (
strict: true); - null and undefined control (
strictNullChecks); - type narrowing (narrowing types through conditions);
- readonly / const for data immutability;
- generic types, which guarantee correctness of relationships between types.
6. Why this matters
Type safety:
- prevents errors in production;
- makes code predictable and reliable;
- helps the IDE provide autocomplete and hints;
- makes refactoring and project growth easier;
- increases the longevity of code (fewer hidden bugs over time).
Summary
Type safety is the guarantee that your code works with the correct data types and performs only valid operations.
Thanks to this, TypeScript:
- catches errors before the program runs,
- makes code reliable, readable, and predictable,
- and protects the project from many small (and costly) bugs.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.