En bref. On écrit : Utilisateur et on se sent protégé. À l’exécution, cette annotation n’existe plus.
Le modèle en une phrase
Les types prouvent à la compilation, puis s’effacent : à l’exécution, il ne reste que les valeurs.
In short. You write : Utilisateur and feel protected. At runtime, that annotation no longer exists.
The model in one sentence
Types prove at compile time, then erase: at runtime, only values remain.
const u: Utilisateur = JSON.parse(brut)typeof u // "object"u.nom.toUpperCase() // TypeError
const u: Utilisateur = JSON.parse(brut)— À la compilation, tsc voit l'annotation : Utilisateur. JSON.parse renvoie any, donc rien ne contredit la promesse.At compile time, tsc sees the annotation: Utilisateur. JSON.parse returns any, so nothing contradicts the promise.typeof u // "object"— À l'exécution, l'annotation n'existe plus : il ne reste qu'un objet ordinaire. Le type a été effacé avant de tourner.At runtime, the annotation no longer exists: only an ordinary object remains. The type was erased before running.u.nom.toUpperCase() // TypeError— u.nom vaut undefined, et l'appel plante. L'annotation : Utilisateur n'a rien vérifié ; les types prouvent à la compilation, puis s'effacent.u.nom is undefined, and the call crashes. The : Utilisateur annotation verified nothing; types prove at compile time, then erase.
La preuve, avec du code qui tourne
Une donnée JSON qui n’a pas la forme promise, annotée comme si elle l’avait :
interface Utilisateur { nom: string; age: number }
const brut = '{"prenom":"Ada"}'; // pas la forme promise
const u: Utilisateur = JSON.parse(brut); // compile sans un cillement
console.log("typeof à l'exécution :", typeof u); // les types n'existent plus
console.log("u.nom =", u.nom); // undefined : l'annotation n'a rien vérifié
try {
console.log(u.nom.toUpperCase());
} catch (e) {
console.log("plantage :", (e as Error).constructor.name);
}typeof à l'exécution : object
u.nom = undefined
plantage : TypeErrorLe fichier compile sans erreur en strict avec tsc 6 : JSON.parse renvoie any, et l’annotation est acceptée. À l’exécution, typeof dit seulement « object », nom vaut undefined, et l’appel plante avec un TypeError. L’annotation n’a rien vérifié.
Ce que l’image cache
L’effacement n’est pas un défaut : c’est ce qui rend TypeScript sans coût à l’exécution. Mais il impose de vérifier à la frontière, là où la donnée entre, avec du code qui existe vraiment à l’exécution. Le type seul ne suffit jamais à valider une donnée externe.
Pour aller plus loin
Le numéro ci-dessous détaille ce qui survit à la compilation, ce qui disparaît, et pourquoi une annotation n’est pas une validation.
The proof, with running code
A JSON value that doesn’t have the promised shape, annotated as if it did:
interface Utilisateur { nom: string; age: number }
const brut = '{"prenom":"Ada"}'; // not the promised shape
const u: Utilisateur = JSON.parse(brut); // compiles without a blink
console.log("typeof à l'exécution :", typeof u); // types no longer exist
console.log("u.nom =", u.nom); // undefined: the annotation verified nothing
try {
console.log(u.nom.toUpperCase());
} catch (e) {
console.log("plantage :", (e as Error).constructor.name);
}typeof à l'exécution : object
u.nom = undefined
plantage : TypeErrorThe file compiles with no error in strict mode with tsc 6: JSON.parse returns any, and the annotation is accepted. At runtime, typeof only says “object”, nom is undefined, and the call crashes with a TypeError. The annotation verified nothing.
What the picture hides
Erasure isn’t a flaw: it’s what makes TypeScript free at runtime. But it forces you to check at the boundary, where data enters, with code that really exists at runtime. The type alone never validates external data.
Go deeper
The issue below details what survives compilation, what disappears, and why an annotation isn’t a validation.