Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
How to throw typescript error in express?

I created a typescript class controller. I intentionally passed a missing property which is "email" in the payload. And I wonder why its not throwing a typescript error like "email(undefined) is not equals to email(string)".

The problem is it did not literally throwed an error because it proceeds executing "console.log". My expectation is, it should not proceed the execution since it did not meet to type "testType" (correct me if i'm wrong).

May I know your thoughts on how to achieve this one?


type testType = {
  body: {
    email: string
    password: string
  }
}

class Testing {
  public static sampleFunc = async (req: testType, res: Response, next: NextFunction) => {
    const temp = req.body
    
    console.log('temp', temp);

    // ..more code here
    
    res.send('success');
  }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Typescript is used for static type checking and is compiled to javascript. Therefore, at runtime you're actually running the compiled javascript code, and as such you cannot rely on typescript to protect you against passing properties with incorrect type.

To achieve what you want, you need to add some additional error checking in the code before you process the request.

Something like:

class Testing {
  public static sampleFunc = async (req: testType, res: Response, next: NextFunction) => {    
    if (!req.email || !req.password) {
      throw new Error("No email / password provided");
    }
    
    res.send('success');
  }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!