Let's consider the following scenario:
const client: Client | boolean = await db.connect()
if (client === false) {
return
}
await start(client)
The function db.connect()
returns either a Client
on successful connection or false
in case of failure.
The function start
requires a parameter of type Client
:
const start = async (dbClient: Client): Promise<void> => {
However, an error occurs when using await start(client)
:
Argument of type true | Client is not assignable to type Client
To address this issue, one option could be to modify the types in start
to handle both Client
and boolean
, but this approach may lead to complications in the future.
An alternative suggestion is to adjust the return value of connect
to an object like this:
{
connected: boolean,
client: client,
}
Would this solution be more suitable for this situation?