简体   繁体   中英

How to fix eslint error in typescript NodeJs: 'Missing return type on function'

I'm working on API based on NodeJs, GraphQl and using Typescript. My app using eslint to mange style issues and I cannot understand how to rid off of the next one:

Missing return type on function.eslint(@typescript-eslint/explicit-function-return-type)

That message is shown in my VSC editor in the following code:

import { GraphQLScalarType, GraphQLError } from 'graphql';
import { Kind } from 'graphql/language';

export default new GraphQLScalarType({
    name: 'Date',
    description: 'Date type',
    parseValue(value) {
        // value comes from the client
        return new Date(value); // sent to resolvers
    },
    serialize(value): Promise<string> {
        // value comes from resolvers
        return value.toISOString(); // sent to the client
    },
    parseLiteral(ast) {
        // ast comes from parsing the query
        // this is where you can validate and transform
        if (ast.kind !== Kind.STRING) {
            throw new GraphQLError(`Query error: Can only parse dates strings, got a: ${ast.kind}`, [ast]);
        }
        if (isNaN(Date.parse(ast.value))) {
            throw new GraphQLError(`Query error: not a valid date`, [ast]);
        }
        return new Date(ast.value);
    },
});

The above code is a custom scalar for the date for the GraphQL to have the date in the right way.

I was able to rid off the message error on this line:

serialize(value): Promise<string> {...}

I just added Promise<string>

I would like to get help about what should be the right approach on the above code.

Presumably the problem is that you are missing a return type for parseValue() and parseLiteral() . So you should be able to just do:

parseValue(value): Date {

...

parseLiteral(ast): Date {

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM