[英]How to fix eslint error in typescript NodeJs: 'Missing return type on function'
我正在開發基於NodeJs,GraphQl並使用Typescript的API。 我的應用使用eslint處理樣式問題,但我不明白如何擺脫下一個問題:
Missing return type on function.eslint(@typescript-eslint/explicit-function-return-type)
該消息顯示在我的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);
},
});
上面的代碼是GraphQL的日期的自定義標量,以正確的方式顯示日期。
我能夠擺脫這一行的消息錯誤:
serialize(value): Promise<string> {...}
我剛剛添加了Promise<string>
我想獲得有關上述代碼正確方法的幫助。
大概的問題是您缺少parseValue()
和parseLiteral()
的返回類型。 因此,您應該能夠做到:
parseValue(value): Date {
...
parseLiteral(ast): Date {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.