8

I'm trying to move to typescript with my Node/Express app. Previously my code was:

//app.js
const error = new Error('Not Found');
error.status = 404;

When I try this:

//app.ts
const error = new Error('Not Found');
error.status = 404; // Property 'status' does not exist on type 'Error'.ts(2339)

I understand from developer.mozilla.org documentation that the Error constructor has the following optional parameters: message, options, fileName, lineNumber - so I guess status shouldn't be permitted? I think I've copied it from a youtube tutorial so I guess it's not actually good practice?

9
  • 1
    Does this answer your question? How do you explicitly set a new property on `window` in TypeScript? Commented Aug 26, 2021 at 16:15
  • 1
    @HereticMonkey: I think that is a different use case, because that is essentially about modifying global scope. Commented Aug 26, 2021 at 16:16
  • @H.B. I think it's the same use case, as it's about adding a property to a built-in type. But take your pick: google.com/…. There's 46k entries there. Commented Aug 26, 2021 at 16:18
  • @HereticMonkey The difference is that with a global you would want to adjust the type information to accommodate the property everywhere whereas here is a special kind of class instance where you usually do not want to affect all instances of that class. But yes, there are probably tons of duplicates that would fit this question. Commented Aug 26, 2021 at 16:22
  • Thanks @HereticMonkey - I've posted my own solution which is specifically for handling 404 error codes. I assume the solutions you've linked to would work for a more general use case. Commented Aug 26, 2021 at 16:23

2 Answers 2

5

TypeScript does not allow adding unknown properties. There are ways to define objects with arbitrary keys (e.g. Record).

In this case you could create your own subclass to Error which includes the status property.

class StatusError extends Error {
  status: number | undefined;
}

const e = new StatusError('Not found');
e.status = 404;

You could also add it to the constructor, then you can confidently remove the undefined.

class StatusError extends Error {
  constructor(public status: number, message?: string) {
    super(message)
  }
}

const e = new StatusError(404, 'Not found');
Sign up to request clarification or add additional context in comments.

3 Comments

Or status?: number;
This is one of the most common errors in TypeScript. Do we need yet another Q&A pair on the subject?
Thanks @H.B. I've posted a solution that's fixed the problem for me, but assume yours would have worked as a viable alternative, and will hopefully help others in a similar situation!
0

I found in the expressjs.com documentation there is a section on "How do I handle 404 responses?", where they provide this example:

app.use(function (req, res, next) {
  res.status(404).send("Sorry can't find that!")
})

So I've produced this and it has stopped the error:

import express, {NextFunction, Request, Response} from "express";

const app = express();
...

app.use((req: Request, res: Response, next: NextFunction) => {
    res.status(404).send("Sorry can't find that!");
});

export { app };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.