yeah, TS will try to infer the properties and type...
# random
g
yeah, TS will try to infer the properties and types of them, based on what's been declared by the user (meaning you). in your example,
args
seems to be an object, and you're trying to access the property
where
and the property
id
from
where
which seems to be another object. For this then you would need to declare the type at the parameter level so that TS knows what to expect when you want to use
args
e.g. type declaration in line:
Copy code
.... = async ({  args: { where: { id: number } }  }) => {
...
}
Or:
Copy code
type MyWhere = {
   id: number;
};

type MyArgs = {
   where: MyWhere;
};

.... = async ({  args: MyArgs }  }) => {
...
}
hope it makes sense 🙂 I'd highly suggest to take on a TS course, this is interactive one: https://www.codecademy.com/learn/learn-typescript
j
Hi @Gustavo... thank you for answering… Are you sure that type can be declared at the parameter level here? arg parameter is already a type of:
_export_ interface ArgsDictionary {
[argName: string]: any;
}
g
ooh yep, in this case the error is at the
where
part not the
args
, so basically TS is unsure whether there will be a where inside your dictionary, if you're 100% sure it'll be there try adding a
!
which is a way to tell TS I know this property will be there.
Copy code
{
    id: args.where!.id
}
A better approach tho, would be to use a type safe dictionary if you have control over it, here's an example: https://blog.logrocket.com/building-type-safe-dictionary-typescript/ Simply scroll down 'till you find
const dictionary: { [key: string]: string } = {};
but I'd recommend reading the whole article 🙂
j
!
which is a way to tell TS I know this property will be there
This is also not allowed according to eslint rules 😄 Now I realised that arguments of resolvers generated by prisma-typegraphql can be mapped directly:
Copy code
const posts = await context.prisma.post.findMany({
    where: args.where as object,
  });
And so the error is also gone. Thank you for your input… helped a lot!
🙌 1