I'm getting an error that I can only fix with adding any
as the return value.
export const dbConnections: any = {};
export const connectDb: Promise<void> = async () => {
if (dbConnections.isConnected) {
return;
}
try {
const db = await mongoose.connect(config.get('mongoURI'), {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});
dbConnections.isConnected = db.connections[0].readyState;
} catch (err) {
createError('Error caught connecting to db!', err);
}
};
This throws an error,
export const connectDb: Promise<void> = async () => {
^^^^^^^^^^^^^
Type '() => Promise<void>' is missing the following properties
from type 'Promise<void>': then, catch, [Symbol.toStringTag], finally
If I do any
instead of Promise<void>
, then the error disappears, but that's obviously not how I'm trying to go about this. How can I fix this lint error?
Issue is in function declaration. You need to give the return type as Promise<void>
.
export const connectDb = async (): Promise<void> => {
if (dbConnections.isConnected) {
return;
}
try {
const db = await mongoose.connect(config.get('mongoURI'), {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});
dbConnections.isConnected = db.connections[0].readyState;
} catch (err) {
createError('Error caught connecting to db!', err);
}
};
Asynchronous functions in typescript return promise value.
like this:
export const dbConnections: any = {};
export const connectDb: () => Promise<void> = async () => {
...
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.