Most Common Typescript Errors and How to Solve Them

Avatar photo

By George

3 min read
Bookmark this post

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.

DigitalOcean Referral Badge
guest
0 Comments
Inline Feedbacks
View all comments

Continue reading

Front, Programming

Learn How to Select Only the CSS First Child

Front, Programming

The CSS nth-of-type Selector – How To Target Elements By Type

Front, Programming

What You Need to Know About CSS nth child Selector – A Practical Guide

Front, Programming

CSS Selectors nth-child VS nth-of-type And How To Use Them Correctly

Front, Programming

Coding Made Easy With CSS Selectors: An Ultimate Guide

Front, Programming

How to Center in CSS Using Different Ways

Front, Programming

What You Need to Know About CSS Color Methods

Front, Programming

Make Your Work Easier by Using Variables in CSS

Front, Programming

CSS Rainbow Text: How To Make Astonishing And Vibrant Fonts

Front, Programming

Most Useful HTML Elements for Maximizing Better Results

Back, Programming

Environment Variables 101: Secure Your Secrets Like a Pro

Front, Programming

HTML Formatting Elements Made Simple: With Easy And Practical Examples

Subscribe to our newsletter

Dive into the Fun Side of Tech! Posts, News, and More Delivered to Your Inbox!

Intuit Mailchimp