typescript
type_error
ai_generated
true
error TS18046: 'x' is of type 'unknown'
ID: typescript/ts18046-unknown-type
95%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 5 | active | — | — | — |
Root Cause
Catch blocks in TS have unknown error type by default (useUnknownInCatchVariables).
genericWorkarounds
-
95% success Narrow the type with instanceof check: if (error instanceof Error) { error.message }
catch (error) { if (error instanceof Error) { console.log(error.message); } }Sources: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#instanceof-narrowing
-
88% success Use type predicate or assertion function for custom error types
Use type predicate or assertion function for custom error types
Sources: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
Dead Ends
Common approaches that don't work:
-
Set useUnknownInCatchVariables to false in tsconfig
70% fail
Disables a useful safety feature
-
Cast error to any
65% fail
Loses type safety — use proper narrowing instead