admin管理员组文章数量:1130349
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
connectDbnot 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
connectDband what you actually have forconnectDbdoesn'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 () => {
...
};
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
connectDbnot 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
connectDband what you actually have forconnectDbdoesn'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 () => {
...
};
本文标签:
版权声明:本文标题:javascript - ESlint error, Type '() => Promise<void>' is missing the following properties f 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1741740251a1885365.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论