I'm attempting to apply this TypeScript example: https://www.apollographql.com/docs/apollo-server/schema/custom-scalars#example-the-date-scalar
import { GraphQLScalarType, Kind } from 'graphql';
export const dateScalar = new GraphQLScalarType({
name: 'Date',
description: 'Date custom scalar type',
serialize(value: Date) {
return value.getTime(); // Convert outgoing Date to integer for JSON
},
parseValue(value: number) {
return new Date(value); // Convert incoming integer to Date
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
// Convert hard-coded AST string to integer and then to Date
return new Date(parseInt(ast.value, 10));
}
// Invalid hard-coded value (not an integer)
return null;
},
});
However, I am encountering TypeScript errors:
src/graphql-scalars/date-scalar.ts:6:5 - error TS2322: Type '(value: Date) => number' is not assignable to type 'GraphQLScalarSerializer<number>'.
Types of parameters 'value' and 'outputValue' are incompatible.
Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
6 serialize(value: Date) {
~~~~~~~~~
node_modules/graphql/type/definition.d.ts:363:3
363 serialize?: GraphQLScalarSerializer<TExternal>;
~~~~~~~~~
The expected type comes from property 'serialize' which is declared here on type 'Readonly<GraphQLScalarTypeConfig<Date, number>>'
Being new to TypeScript, I am unsure how or where to define or extend these types. Can anyone provide guidance?