Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructure a function parameter in Typescript

I'm trying to give a type to the members of below function. args is an object with a data property of type UserCreateInput

So, from this:

createUser(parent: any, args: any, context: any) {
    return context.prisma.createUser(args.data)
}

I wrote this:

createUser(parent: any, args: {data: UserCreateInput}, context: any) {
    return context.prisma.createUser(args.data)
}

I'm not sure how to replace 'xxx' in createUser(parent: any, xxx, context: any) so I can simply return return context.prisma.createUser(data)

like image 570
Greg Forel Avatar asked Feb 04 '19 09:02

Greg Forel


1 Answers

You can use the object de-structuring syntax:

createUser(parent: any, { data }: { data: UserCreateInput }, context: any) {
    return context.prisma.createUser(data)
}

Unfortunately it is required you write data twice. There is a proposal to fix this but there are issues around this.

like image 50
Titian Cernicova-Dragomir Avatar answered Oct 15 '22 06:10

Titian Cernicova-Dragomir