I'm converting a JS project to TS and here I have this error (using urql):
A function whose declared type is neither 'void' nor 'any' must return a value. ts(2355) on line:
playerCreate: (result, args, cache): UpdateResolver => {
Why?
const updates = {
Mutation: {
playerCreate: (result, args, cache): UpdateResolver => {
const playersQueries = cache
.inspectFields("Query")
.filter((x) => x.fieldName === "players");
playersQueries.forEach(({ fieldName, arguments: variables }) =>
cache.invalidate("Query", fieldName, variables)
);
},
playerDelete: (result, args, cache, info): UpdateResolver => {
// using result, args, cache here
},
},
};
I can see Updateresolver is declared like this:
export declare type UpdateResolver = (result: Data, args: Variables, cache: Cache, info: ResolveInfo) => void;
UPDATE:
Someone rightly told me that I'm saying that this function returns an UpdateResolver while the type is for the function not the return-type.
Hence the question:
How can I correctly type here playerCreate and playerDelete?
playerCreatemethod, then return it, and then change theUpdateResolvertype to match it. It's not clear what JS logic you want there.result, args, cache, info. I wanna tell it: "that function type isUpdateResolver!". Only I don't know how...{ foo: "hello" }and{ bar: () => {} }are syntactically equivalent: an object with a property where you assign a value to the property. The duplicate shows how you can give such a thing a type - either type the object or do a type assertion. The latter is is the same as the answer you already got.const updates: {Mutation: Record<string, UpdateResolver>} = {...and remove the types from the individual functions. You can also write the functions outside of the object and piece it together.const playerCreate: UpdateResolver = (....thenconst updates = { Mutation: { playerCreate, playerDelete }, }, };The problem is that you can't set a property and declare the type for that property at the same time without usingasassertion (which can be dangerous since it is an override, not a check).