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?
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?
-
1
Because you're assigning a function to
connectDb
not a promise. Either you need the type to be() => Promise<void>
or change it so the value is the executed function (perhaps by using an IIFE). – VLAZ Commented Sep 29, 2020 at 7:27 -
1
Also, it's not a lint error - it's a piler error. TS is doing its job correctly by alerting you that what you want to have as
connectDb
and what you actually have forconnectDb
doesn't match. – VLAZ Commented Sep 29, 2020 at 7:29 -
Setting it to
export const connectDb = async (): Promise<void> => {
worked, thank you. I just want to note, that this is an existing project with TS, and only an hour ago I decided to add ESLint to it following this tutorial, and that's when I started getting this error. Thanks again – Mike K Commented Sep 29, 2020 at 7:34 - 1 I'm getting this error when I try the code in the Playground Link – VLAZ Commented Sep 29, 2020 at 7:35
2 Answers
Reset to default 3Issue 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 () => {
...
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741740258a4363444.html
评论列表(0条)