Javascript 如何在日期标量中键入parseLiteral参数?
如何键入Javascript 如何在日期标量中键入parseLiteral参数?,javascript,typescript,graphql,nestjs,Javascript,Typescript,Graphql,Nestjs,如何键入ast import { CustomScalar, Scalar } from '@nestjs/graphql' import { Kind } from 'graphql' export class DateScalar implements CustomScalar<number, Date> { description = 'Date custom scalar type' parseValue(value: number): Date { ret
ast
import { CustomScalar, Scalar } from '@nestjs/graphql'
import { Kind } from 'graphql'
export class DateScalar implements CustomScalar<number, Date> {
description = 'Date custom scalar type'
parseValue(value: number): Date {
return new Date(value) // value from the client
}
serialize(value: Date): number {
return value.getTime() // value sent to the client
}
parseLiteral(ast): Date { // <-- ???
if (ast.kind === Kind.INT) {
return new Date(ast.value)
}
return null
}
}
但我确实得到了错误
Property 'parseLiteral' in type 'DateScalar' is not assignable to the same property in base type 'CustomScalar<number, Date>'.
Type '(ast: AST) => Date' is not assignable to type 'GraphQLScalarLiteralParser<Date>'.
Types of parameters 'ast' and 'valueNode' are incompatible.
Type 'ValueNode' is not assignable to type 'AST'.
Property 'value' is missing in type 'VariableNode' but required in type 'AST'
类型“DateScalar”中的属性“parseLiteral”不能分配给基类型“CustomScalar”中的相同属性。
类型“(ast:ast)=>Date”不可分配给类型“GraphQLScalarLiteralParser”。
参数“ast”和“valueNode”的类型不兼容。
类型“ValueNode”不可分配给类型“AST”。
类型“VariableNode”中缺少属性“value”,但类型“AST”中需要属性“value”
好的,我们可以看到parseLiteral
是graphqlscalariteralparser
中的类型graphql
。我发现这个定义,其中ast
也称为ValueNode
。它包含像NullValueNode
和ObjectValueNode
这样的东西,这些东西没有值作为属性,因此会出现错误
所有这些前言和链接都是这样说的:如果您想键入它,请使用“graphql”中的import{ValueNode}
并使用类型ValueNode
。通过使用ast.Kind===Kind.INT
将ast
强制转换为IntValueNode
,该节点将字段value
作为属性。应该解决你遇到的任何类型的问题
Property 'parseLiteral' in type 'DateScalar' is not assignable to the same property in base type 'CustomScalar<number, Date>'.
Type '(ast: AST) => Date' is not assignable to type 'GraphQLScalarLiteralParser<Date>'.
Types of parameters 'ast' and 'valueNode' are incompatible.
Type 'ValueNode' is not assignable to type 'AST'.
Property 'value' is missing in type 'VariableNode' but required in type 'AST'