简体   繁体   English

如何修复打字稿NodeJ中的eslint错误:“函数中缺少返回类型”

[英]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. 我正在开发基于NodeJs,GraphQl并使用Typescript的API。 My app using eslint to mange style issues and I cannot understand how to rid off of the next one: 我的应用使用eslint处理样式问题,但我不明白如何摆脱下一个问题:

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

That message is shown in my VSC editor in the following code: 该消息显示在我的VSC编辑器中的以下代码中:

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. 上面的代码是GraphQL的日期的自定义标量,以正确的方式显示日期。

I was able to rid off the message error on this line: 我能够摆脱这一行的消息错误:

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

I just added Promise<string> 我刚刚添加了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() . 大概的问题是您缺少parseValue()parseLiteral()的返回类型。 So you should be able to just do: 因此,您应该能够做到:

parseValue(value): Date {

...

parseLiteral(ast): Date {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误在nodejs中缺少函数的返回类型 - error Missing return type on function in nodejs Jest / eslint 中的 function 缺少返回类型 - Missing return type on function in Jest / eslint 如何使用 type.pfx 证书修复 nodejs 中缺少中间/链证书 - How to fix missing an Intermediate/chain certificate in nodejs with certificate of type .pfx 如何解决Typescript编译错误ts2345“类型&#39;Response&#39;丢失…从类型&#39;Response&#39;中:重定向,预告片,formData!” - How to fix Typescript compilation error ts2345 “Type 'Response' is missing … from type 'Response': redirected, trailer, formData!” 如何使用nodejs修复这个foreach函数错误 - How to fix this foreach function error with nodejs 如何修复 nodeJS 错误:parseFileSync() 不是 function - How to fix nodeJS error: parseFileSync() is not a function 如何修复POST不是NodeJS中的功能错误 - How to fix POST is not function error in NodeJS 如何解决这不是一个功能nodejs - How to fix this is not a function nodejs VSCode 如何修复 typescript 突出显示错误 {} 类型的参数不可分配给 - VSCode how to fix typescript highlight error Argument of type {} is not assignable to parameter of 我的 typegoose 保存 function 没有返回需要的类型(NodeJs,typescript,graphql) - my typegoose save function didn't return needed type (NodeJs, typescript, graphql)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM