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`.
BashReason 🔬
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) => {
BashReason 🔬
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!');
})
TypeScriptMy 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
BashIn 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}`);
});
TypeScriptTS7016 – 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.
BashReason 🔬
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 '[]'.
BashReason 🔬
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.