javascript - ESlint error, Type '() => Promise<void>' is missing the following properties f

I'm getting an error that I can only fix with adding any as the return value.export const dbConne

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?

Share Improve this question asked Sep 29, 2020 at 7:24 Mike KMike K 6,51117 gold badges75 silver badges144 bronze badges 4
  • 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 for connectDb 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
Add a ment  | 

2 Answers 2

Reset to default 3

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 () => {
    ...
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741740258a4363444.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信