TypeScript Reference ยท Error Codes Worth Knowing
TypeScript error TS18046 (formerly TS2571): value is of type unknown
What TS18046 and its older code TS2571 mean in TypeScript, why unknown values and catch variables cannot be used directly, and how to narrow them safely.
TS18046 reads 'x' is of type 'unknown'. It appears when you use a value whose type is unknown in a way that needs a more specific type: calling a method, reading a property, calling it as a function, or doing arithmetic. Older compilers reported the same problem as TS2571, Object is of type 'unknown'. Current compilers include the variable name and use the newer code, so searching for either number leads here.
unknown is the type-safe counterpart of any. You may put anything into it, but you may do nothing with it until you prove what it is. The error is the compiler doing its job, not a bug to silence.
function shout(value: unknown) { return value.toUpperCase(); // error TS18046: 'value' is of type 'unknown'. }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
The error is a diagnostic, not syntax. Where unknown typically comes from:
| Source | Why it is unknown |
|---|---|
catch (e) |
With useUnknownInCatchVariables, part of strict, anything can be thrown. |
Parameters you declare as unknown |
You chose the safe top type on purpose. |
Response.json()-style wrappers you typed as Promise<unknown> |
Data from outside the program is untrusted. |
Generic defaults such as T = unknown |
No better information is available. |
Fixes, from best to worst:
| Fix | Example |
|---|---|
Narrow with typeof, instanceof, in, or a custom guard |
if (e instanceof Error) { ... } |
| Validate the shape, then use a type predicate | function isUser(x: unknown): x is User |
| Assert after you checked another way | value as User |
| Give the variable a proper type at the source | function parse(text: string): Config |
Usage notes
- Never reach for
anyfirst. Writingcatch (e: any)makes the error vanish and also removes every check one. Narrow instead. - Thrown values are not always errors. JavaScript lets you throw strings, numbers, or plain objects, which is why
catchvariables areunknown. typeof x === "object"includesnull. Testx !== nullas well before reading properties.- The
inoperator narrows objects: after"count" in parsed, the property is known to exist, though its own type isunknown. - Assertions are promises, not checks.
value as Usercompiles and runs even when the data is wrong. Prefer a runtime validator for data from outside. - Same idea, different code: TS2571 became TS18046 when messages started naming the variable. Both mean the same thing.