TRPC type issue

I have a typing issue which I don't understand, I have this trpc create method
create: t.procedure
.input(createFileSchema)
.mutation(async ({ input, ctx }) => {
const userId = ctx.session?.user?.id

if (!userId) {
throw new Error('Not authenticated')
}

const file = await prisma?.file.create({
data: { // error
User: {
connect: {
id: userId,
},
},
...input,
},
})

return file
}),
create: t.procedure
.input(createFileSchema)
.mutation(async ({ input, ctx }) => {
const userId = ctx.session?.user?.id

if (!userId) {
throw new Error('Not authenticated')
}

const file = await prisma?.file.create({
data: { // error
User: {
connect: {
id: userId,
},
},
...input,
},
})

return file
}),
But I get this type error
Types of property 'userId' are incompatible.
Type 'string' is not assignable to type 'undefined'.ts(2322)
index.d.ts(5945, 5): The expected type comes from property 'data' which is declared here on type
Types of property 'userId' are incompatible.
Type 'string' is not assignable to type 'undefined'.ts(2322)
index.d.ts(5945, 5): The expected type comes from property 'data' which is declared here on type
This is the schema:
const createFileSchema = z.object({
id: z.string().nullish(),
userId: z.string(),
url: z.string().min(1, 'Url is required.'),
title: z.string().min(1, 'Title is required.'),
description: z.string().min(1, 'Description is required.').nullish(),
createdAt: z.string().nullish(),
updatedAt: z.string().nullish(),
})
const createFileSchema = z.object({
id: z.string().nullish(),
userId: z.string(),
url: z.string().min(1, 'Url is required.'),
title: z.string().min(1, 'Title is required.'),
description: z.string().min(1, 'Description is required.').nullish(),
createdAt: z.string().nullish(),
updatedAt: z.string().nullish(),
})
3 Replies
theo (t3.gg)
theo (t3.gg)2y ago
You might not have strict on in tsconfig
utdev
utdev2y ago
strict is on, strange
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server