Adding Types to an Existing Project

Gradually. All at once is how migrations fail and get reverted.

allowJs lets typed and untyped files coexist. Rename one file to .ts, fix what appears, commit, repeat. Type the boundaries first, because that is where the payoff is: API responses, shared utilities, anything used in many places.

@ts-expect-error rather than @ts-ignore. Both silence the next line. The difference is that @ts-expect-error becomes an error itself once the problem is fixed, so it cleans itself up. @ts-ignore stays forever, silently, long after it stopped being needed.

Third-party types. Most libraries ship their own. If an import errors with 'could not find a declaration file', try the @types package for it. A library with no types at all can be declared minimally in a .d.ts rather than left as any.

Try it

Consider what would happen if that returned any instead. Everything downstream would be unchecked and silent.

loading editor…

checking…

Declaring the return as unknown rather than any is the right call: it makes the boundary explicit rather than pretending you know the shape.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back