En bref. Une valeur dont on ignore la forme existe forcément. Reste à choisir comment la déclarer : en faisant semblant de savoir, ou en le disant.
Le modèle en une phrase
unknown dit honnêtement qu’on ne sait pas, et refuse tout usage tant qu’un test n’a pas prouvé le type.
In short. A value whose shape you don’t know necessarily exists. What remains is how to declare it: pretending to know, or saying so.
The model in one sentence
unknown honestly says you don’t know, and refuses any use until a test has proven the type.
x.toUpperCase() // erreur : x est unknowntypeof x === "string" // x : stringlire({ a: 1 }) // "inconnu"
x.toUpperCase() // erreur : x est unknown— Un unknown ne laisse rien faire : tant que le type n'est pas prouvé, tsc refuse l'accès.An unknown lets you do nothing: as long as the type isn't proven, tsc refuses access.typeof x === "string" // x : string— Après un test typeof, tsc sait que x est string : toUpperCase est permis.After a typeof test, tsc knows x is string: toUpperCase is allowed.lire({ a: 1 }) // "inconnu"— Une valeur qu'aucun test ne reconnaît reste unknown : on s'abstient plutôt que de deviner.A value no test recognizes stays unknown: we abstain rather than guess.
La preuve, avec du code qui tourne
Une fonction qui accepte n’importe quoi et ne fait rien sans preuve :
function lire(x: unknown): string {
// @ts-expect-error x est unknown : toUpperCase refusé sans preuve
if (false) return x.toUpperCase();
if (typeof x === "string") return x.toUpperCase(); // preuve : x est string
if (typeof x === "number") return x.toFixed(1); // preuve : x est number
return "inconnu"; // aucune preuve : on s'abstient
}
console.log(lire("ada"));
console.log(lire(3.14159));
console.log(lire({ a: 1 }));
// any aurait tout laissé passer ; unknown force à prouver d'abord.
const entree: unknown = JSON.parse('["x", 2]');
console.log(Array.isArray(entree) ? "tableau de " + entree.length : "autre chose");ADA
3.1
inconnu
tableau de 2Le fichier compile en strict avec tsc 6. L’accès direct à toUpperCase sur un unknown est refusé, ts-expect-error le prouve. Après typeof, la branche string et la branche number sont permises. Une valeur qu’aucun test ne reconnaît donne inconnu : on s’abstient plutôt que de deviner.
Ce que l’image cache
unknown ne valide rien à l’exécution : il oblige seulement à écrire un test. Si ce test est faux ou incomplet, la preuve l’est aussi, comme pour un type guard menteur. C’est donc une bonne frontière, pas une garantie : la rigueur reste dans les tests que l’on écrit.
Pour aller plus loin
Le numéro ci-dessous détaille unknown face à any, le raffinement par tests, et pourquoi c’est le bon type pour une donnée externe.
The proof, with running code
A function that accepts anything and does nothing without proof:
function lire(x: unknown): string {
// @ts-expect-error x est unknown : toUpperCase refusé sans preuve
if (false) return x.toUpperCase();
if (typeof x === "string") return x.toUpperCase(); // proof: x is string
if (typeof x === "number") return x.toFixed(1); // proof: x is number
return "inconnu"; // no proof: we abstain
}
console.log(lire("ada"));
console.log(lire(3.14159));
console.log(lire({ a: 1 }));
// any would have let everything through; unknown forces you to prove first.
const entree: unknown = JSON.parse('["x", 2]');
console.log(Array.isArray(entree) ? "tableau de " + entree.length : "autre chose");ADA
3.1
inconnu
tableau de 2The file compiles in strict mode with tsc 6. Direct access to toUpperCase on an unknown is refused, ts-expect-error proves it. After typeof, the string branch and the number branch are allowed. A value no test recognizes gives inconnu: we abstain rather than guess.
What the picture hides
unknown validates nothing at runtime: it only forces you to write a test. If that test is wrong or incomplete, so is the proof, as with a lying type guard. It’s a good boundary, not a guarantee: rigor remains in the tests you write.
Go deeper
The issue below details unknown versus any, refinement by tests, and why it’s the right type for external data.