Home Β» Blog Β» Most Common Typescript Errors and How to Solve Them
Guy sitting besides a server in trouble because of errors shown while configuring it

Most Common Typescript Errors and How to Solve Them

While learning Typescript, I encountered many issues that were either recurring or difficult to solve. Because of this, I decided to share them in this post so that I could have access to the most common Typescript errors and their solutions. Since these are my first steps in learning TypeScript, feel free to share your insights and correct me if you see a solution applied incorrectly.

I will occasionally add more typescript errors to this post, hoping to save time and spare at least one of you the frustration and effort of dealing with them!

TS2580 – Cannot find name ‘require’

Error πŸ†˜ (TS2580)

error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
Bash

Reason πŸ”¬

As the error description clearly suggests, Typescript needed @types/node to successfully finish the compilation.

Solution βœ…

Running npm i --save-dev @types/node solves this issue

TS7006 – Parameter ‘res’ implicitly has an ‘any’ type.

Error πŸ†˜

error TS7006: Parameter 'res' implicitly has an 'any' type.6 app.get('/', (req, res) => {
Bash

Reason πŸ”¬

I was building a project and had the following boilerplate code included

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
})
TypeScript

My tsconfig.json file had compilerOptions: {"strict": true } set, so it was basically telling TS not to accept type any for its parameters. strict: true is a shortcut for applying strict rules, including the noImplicitAny rule.

Solution βœ…

To solve this issue, we need to install @types/express by running

npm install --save-dev @types/express
Bash

In addition, we will refactor our code to use explicit types for our function parameters and constants.

import express, { Express, Request, Response } from 'express';
const app: Express = express();
const port = 3004;

app.get('/', (req: Request, res: Response) => {
  res.send('Hello World!');
})
app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

TypeScript

TS7016 – Could not find a declaration file for module X.file Y implicitly has an ‘any’ type

Error πŸ†˜

error TS7016:  Could not find a declaration file for module X.file Y implicitly has an 'any' type.

Bash

Reason πŸ”¬

This error occurs because module X may not include TypeScript type definitions by default, and type definitions for module X haven’t been installed or declared in your project.

Solution βœ…

If there are community-maintained type definitions, e.g @types/module-x install them, and this will solve your issue.
Another way to overcome this error is to create a custom-type declaration if option 1 is unavailable.

TS18003 – No inputs were found in config file <filename>

While initializing my project, I saw this error when running npx tsc

Error πŸ†˜

error TS18003: No inputs were found in config file '/server-project/tsconfig.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'.
Bash

Reason πŸ”¬

Typescript needs at least one ts file in a project to compile. I was seeing this error cause I was too eager to run the tc compiler without any .tsc files include in my project.

Solution βœ…

Just add a .ts file, even an empty one, for starters, in your project.

If you’re experiencing a recurring error that’s frustrating you, please mention it in the comments section so we can address it and help others who might be facing the same issue.

Leave a reply

Your email address will not be published. Required fields are marked *