#typescript not throwing error for wrong type

1 messages · Page 1 of 1 (latest)

buoyant condor
#
import { Ticket } from '@prisma/client';
const prisma = require('../db');

const router = require('express').Router();

router.get('/all', async (req: Request, res: Response) => {
    try {
        const tickets: Ticket[] = await prisma.Assigned_Ticket.findMany();
        console.log(tickets)
        res.json(tickets)
    } catch (error) {
        console.log(error);
        res.sendStatus(500);
    }
})
module.exports = router;```

I am using express, nodejs, ts. I am trying to return an array of Tickets. I tried changing the type to string, and it still works even though the query will never return a string.
dark vault
#

multiple things:

#
  • don't use require in TypeScript
#

if you need to import something, uses the import {} from "" syntax

#

so in your case import { Router } from "express"

#

this way, TypeScript can know the type of the Router function, which was previously unknown because of the require
if you need a default import, use import prisma from "../db"

#
  • don't use module.exports =
#

if you need to export something in TypeScript, add export in front of the function or variable when you declare it, like so export const router = Router()

#

or if you want a default export const router = Router(); export default router;

#
  • don't use try {} catch() {} with await
#

this one is more a personal opinion, but using .then() and .catch() makes the code much cleaner

#

you can also avoid typying all your functions as async, since you won't be using the await keyword anymore

#
  • don't type your parameters when not needed
    this is a bit of a special case here, since your req and res parameters might have a type that is more than just a simple Request or Response
#

these two types are generic, meaning they can contain much more information
information that would be lost if your re-assign them a type in your route handler

#

also, by omitting those types, the code is more concise and TS can infer the right types just fine

wooden creekBOT
#
Ascor8522#7606

Preview:```ts
// @filename: ../db
interface Ticket {

}
export declare const prisma: {
Assigned_Ticket: {
findMany(): Promise<Ticket[]>;
}
};

// @filename: index
import { Router } from 'express';
import prisma from '../db';

const router = Router();

router.get('/all', (req, res) => {
...```

dark vault
#

this is what the final result looks like

#

but to come back to your original problem...

I am trying to return an array of Tickets. I tried changing the type to string, and it still works even though the query will never return a string.
findMany should return an array, you can't really change that
res.json accepts about anything (any) and tries to turn that object into a JSON string
so since it accepts anything but not everything is serializable, it might throw an error at runtime, but not at compile time

#

also, don't forget calling res.json or res.sendStatus doesn't return anything or doesn't make the code exit the current function, you're just setting a few fields in the response object

#

@buoyant condor

buoyant condor
#

Thank youuu

#

I just jumped into TS so im picking up little bits here and there

dark vault
#

the imports should be one of the few things that are different from JavaScript

#

but for the most part, those rules also apply to JS (import/export in ESM, await vs then, etc.)

buoyant condor
#

Exactly what i was looking for and more

buoyant condor
#

thanksss everything is working as expected now

#

!solved

dark vault
#

!resolved