Mr.Unforgettable
Mr.Unforgettable
TTCTheo's Typesafe Cult
Created by Mr.Unforgettable on 11/3/2024 in #questions
What is the best way to define my own custom types definations?
I am trying to build some application with typescript. I was facing issues with some types like for example setting a status code for internal server error when I catch some error in the try block and setting ./src/controllers/admin.ts
const handleServerError = (error: unknown | any, next: NextFunction) => {
let errorMessage: string;

if (error instanceof Error) {
errorMessage = error.message;
} else {
errorMessage = String(error);
}

const err = new Error(errorMessage);
console.error(err);
err.httpStatusCode = 500;
return next(err);
};
const handleServerError = (error: unknown | any, next: NextFunction) => {
let errorMessage: string;

if (error instanceof Error) {
errorMessage = error.message;
} else {
errorMessage = String(error);
}

const err = new Error(errorMessage);
console.error(err);
err.httpStatusCode = 500;
return next(err);
};
and using it like a call in the catch block:
try {
} catch (error) {
handleServerError(error, next);
}
try {
} catch (error) {
handleServerError(error, next);
}
Since, httpStatusCode does not exists on the type Error. I solved this issue by creating ./src/types/error.d.ts
declare global {
interface Error {
httpStatusCode?: number;
}
}

export {};
declare global {
interface Error {
httpStatusCode?: number;
}
}

export {};
However I have to add this defination file in my tsconfig.json { "compilerOptions": { }, "files": [ "src/types/error.d.ts", ... ] } Is there any better ergonomic way to deal with this situation?
1 replies
TTCTheo's Typesafe Cult
Created by Mr.Unforgettable on 6/30/2024 in #questions
Having difficulty with sequelize association methods.
Hey I am have a problem with the sequelize package. I am try to create an association between the two models 'User' and 'Cart'.
User.hasOne(Cart);
Cart.belongsTo(User);
User.hasOne(Cart);
Cart.belongsTo(User);
Now in my app.ts file i am try to create some dummy user and cart.
(async () => {
try {
await sequelize.sync({ force: true }); // Remove this line in production build.
// await sequelize.sync();

let user = await User.findByPk(1);
if (!user) {
user = await User.create({
name: "TestSubject1",
});
}

console.log("User created:", user);

let cart = await user.createCart();

console.log("Cart created:", cart);

app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
} catch (error) {
console.error("Error during Sequalize sync or user creation:", error);
}
})();
(async () => {
try {
await sequelize.sync({ force: true }); // Remove this line in production build.
// await sequelize.sync();

let user = await User.findByPk(1);
if (!user) {
user = await User.create({
name: "TestSubject1",
});
}

console.log("User created:", user);

let cart = await user.createCart();

console.log("Cart created:", cart);

app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
} catch (error) {
console.error("Error during Sequalize sync or user creation:", error);
}
})();
However, I am getting error stating "createCart" property does not exist on the type 'User'. I was expecting the hasOne or hasMany relation in sequelize to provide me with some special methods. Please correct me i am wrong and help me clearify my doubt.
1 replies