跪拜 Guibai
← All articles
Frontend · JavaScript · TypeScript

The 10 TypeScript Errors You Hit Every Day, Translated Into Plain English

By kyriewen ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

These ten errors surface in nearly every TypeScript project daily. Knowing what each one actually means—and the one-line fix—cuts debugging time and prevents the common reflex of slapping `as any` on code that just needed a type annotation or an `if` guard.

Summary

TypeScript's error messages are famously cryptic, but the underlying rules are consistent. Ten specific errors account for the vast majority of red squiggles in daily frontend work, from `not assignable to type 'never'` to `cannot find module`.

The `never[]` error, for instance, happens because an empty array literal has no elements for TypeScript to infer a type from, so it defaults to the bottom type `never` and rejects any push. The `possibly undefined` error is TypeScript enforcing that values like `Array.find()` returns must be checked before use, preventing runtime `TypeError` crashes.

Each error comes with a concrete fix and a preferred approach. Optional chaining (`?.`) is the default safety net for nullable values, while non-null assertions (`!`) are a last resort. Runtime validation with `if` guards is safer than type assertions when consuming API data, because backends can return unexpected values that assertions silently ignore.

Takeaways
An empty array literal `[]` infers as `never[]`; always annotate it with a type like `string[]`.
`Array.find()` returns `T | undefined`, so accessing its result without a guard triggers `Object is possibly 'undefined'`.
Optional chaining (`?.`) is the safest default for nullable values; non-null assertions (`!`) should only be used when a value was just assigned.
Runtime validation (`if` checks) is safer than type assertions (`as`) when consuming API data, because backends can return unexpected values.
Union types are a whitelist: `'active' | 'inactive'` rejects `'pending'` because it was not declared as an allowed value.
Missing type declarations for npm packages are fixed by installing the corresponding `@types/` package or writing a `.d.ts` declaration file.
Arrow functions inherit `this` from the enclosing scope, avoiding the `'this' implicitly has type 'any'` error that plagues regular functions in callbacks.
Conditional JSX component variables need an explicit `React.ElementType` or `React.FC` type to satisfy TypeScript's JSX validation.
Conclusions

TypeScript's error messages feel opaque because they describe type-level conflicts, not the programmer's intent—translating them into the underlying rule makes the fix obvious.

The `never` type error on empty arrays is a direct consequence of TypeScript's design choice to infer types from initial values; it's a tradeoff between convenience and safety that bites every beginner.

Many TypeScript errors are actually runtime bug prevention: `possibly undefined` stops `TypeError` crashes, and excess property checking catches misspelled keys before they ship.

The community rule 'always type your empty arrays' exists because inference works perfectly for non-empty arrays but fails silently on empty ones—a sharp edge in an otherwise smooth system.

Type assertions on API data are a common source of production bugs because they bypass the very checks TypeScript is designed to enforce; runtime validation should be the default.

Concepts & terms
Bottom type (never)
A type that has no possible values. TypeScript uses `never` to represent code paths that should never execute or, in the case of empty arrays, an array with no permissible element type.
Type narrowing
TypeScript's ability to automatically reduce a union type to a more specific type after a runtime check, such as an `if` guard or a `typeof` check.
Optional chaining (?.)
A JavaScript operator that short-circuits and returns `undefined` if the left-hand side is `null` or `undefined`, instead of throwing a runtime error.
Non-null assertion (!)
A TypeScript postfix operator that tells the compiler a value is definitely not `null` or `undefined`, removing the error without any runtime check. Dangerous if the assumption is wrong.
Declaration file (.d.ts)
A file that describes the types of JavaScript code to TypeScript without containing any executable code. Used to provide type information for untyped npm packages.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗