New to TypeScript. I'm developing a node application using Express, Postgresql and Knex and I'm a little confused in some areas as to when I should and shouldn't need to apply type definitions. I'm looking at other projects for some guidance, and I'm getting a bit confused as to how they are approaching when to use type definitions and when not to.
This is a version of code I saw. Why does this person define a return type Promise<Knex.Transaction> and then also uses type casting? Wouldn't you only need the return type for the function?
database/index.ts:
export default function startTransaction(): Promise<Knex.Transaction> {
return new Promise((resolve, reject) => {
return db
.transaction((trx) => {
return resolve(trx);
})
.catch(() => {
reject('unable to complete transaction');
});
}) as Promise<Knex.Transaction>
}
Is this done correctly below? All I'd be using TypeScript with Knex is is for autocompletion aid for these queries?
model/company.ts:
interface Company {
id: string;
name: string;
date_created: Date;
}
function getCompanies(trx: Knex.Transaction): Knex.QueryBuilder {
return trx.table<Company>('company').returning('*');
}
This might be a dumb question, but for this controller below, you don't need to define' trx' type or 'companies' type because they are already defined in the model, right?
controller/company.ts:
const getCompanies: RequestHandler = async (req, res): Promise<void> => {
const trx = await startTransaction();
try {
const companies = await Test.getCompanies(trx);
await trx.commit();
res.status(200).send(companies);
} catch (error) {
await trx.rollback();
res.status(500).send(error);
}
};
In general, when I am querying the database, do I also need to define the type the database returns back to me? I know when I am receiving client requests, I obviously need to ensure all those values are defined exactly as my database defines them. Thanks! Fundamental questions, but really helpful in me moving forward and understanding completely how I should approach this part of the project.