#error TS2698: Spread types may only be created from object types

30 messages · Page 1 of 1 (latest)

late parcel
#

My code:

`@Patch('/features/:featureType')
async updateFeature(@Req() req: Request, @Param('guild') guild: string, @Body() body: Partial<':featureBod'>, @Param('featureType') featureType: string, @Param('featureBod') featureBod: string) {
this.guilds.checkPermissions(auth(req), guild);

if (featureType === 'welcome-message') {
featureBod === 'WelcomeMessage'
const updated = await this.prisma.welcomeMessage.update({
where: {
id: guild,
},
data: {
...body,
id: undefined,
},
});

  return updated;

} else if (featureType === 'utility') {
featureBod === 'Utility'
const updated = await this.prisma.utility.update({
where: {
id: guild,
},
data: {
...body,
id: undefined,
},
});

return updated;

}
}`

Error: error TS2698: Spread types may only be created from object types.

https://cdn.discordapp.com/attachments/1153102083755610192/1153106906068562020/image.png

#

What I'm trying to do is I want to check if the path is /features/welcome-message, if it already is, then it will be Partial<WelcomeMessage> and if it is /features/utility, then it will be Partial <Utility>

#

This is an image of WelcomeMesssage and the utility within the Prisma client
Of course I would change the properties and number of columns, but I only did this as an example

#

What I'm trying to do is I want to check if the path is /features/welcome-message, if it already is, then it will be Partial<WelcomeMessage> and if it is /features/utility, then it will be Partial <Utility>

sullen mauve
#

What the heck is Partial<':featureBod'>? Is that a Partial of a string literal? I don't recognize the ':featureBod' syntax if that's something specific to Typescript

unborn glade
#

Looks like you're trying to use a part of a Param on the request to create the type hint for the body. I don't believe that will work.

#

TS has to know what types to enforce when the code is not running.

late parcel
#

Yes, it's typeScript
What can I do to resolve this error??

unborn glade
#

You could provide type options with the | separating the different type options.

sullen mauve
#

You need to use an actual type for the @Body() rather than Partial<':featureBod'> because that's equating to ':featureBod' | undefined where ':featureBod' is the string literal ':featureBod', not a type hint or anything like that

#

You'd need something like Partial<WelcomeMessage | Utility | etc> for the @Body()

sullen mauve
#

Dynamically in the type safe manner way you want to? It really won't be easily doable

#

It'd be easier to have separate endpoints for each one

late parcel
#

Is there an example please?

unborn glade
#

Providing options will also lose some of the benefit of the TS enforcement though. I'd suggest restructuring the endpoints where you can strictly define the types expected for each endpoint.

late parcel
sullen mauve
#

Why would the site slowdown? What site?

late parcel
#

It is a control panel for Discord Bot

sullen mauve
#

So what is slowing down specifically?

late parcel
#

When a data update is sent to the database, I feel like it takes a little longer from the beginning

sullen mauve
#

That wouldn''t have anything to do wih the Types. Tthat would 100% be the implementation and maybe the database, not the types you use for static type analysis before the code is compiled

late parcel
#

Well I will review everything myself now...but is this also related to some packages?

sullen mauve
#

What do you mean?

unborn glade
#

I would highly recommend doing more research on what typescript is and what it does. Taking a swing at using it without understanding it can slow you down a lot and usually leads to misuse of it (like working around types with any too much etc)