我无法删除错误 "Type 'User | null' is not assignable to type 'User'. Type 'null' is not assignable to type 'User'"
I cant remove the error "Type 'User | null' is not assignable to type 'User'. Type 'null' is not assignable to type 'User'"
export interface User {
id: string
name: string
bio: string
public_repos: string
public_gists: string
}
async function getPrismaUser(
ctx: Context,
githubUserId: string,
): Promise<User> {
return await ctx.prisma.user.findOne({ where: { githubUserId } })
}
我已经尝试在我的 return 语句中添加非空断言检查,但错误并没有消失。我唯一的解决方案是 "strict": false in tsconfig.json?
findOne()
的 return 类型是 User | null
(有一些条件,见下文)。该联合类型与函数 getPrismaUser()
的 return 类型不匹配,即 User
.
要修复您的错误,请将 getPrismaUser()
的 return 类型更改为 User | null
。
findOne returns a plain old JavaScript object or null.
The type of the object that's returned by a findOne API call depends
on whether you use the select and include options.
If you use neither of these options, the return type will correspond
to the TypeScript type that's generated for the model.
来自Prisma docs.
export interface User {
id: string
name: string
bio: string
public_repos: string
public_gists: string
}
async function getPrismaUser(
ctx: Context,
githubUserId: string,
): Promise<User> {
return await ctx.prisma.user.findOne({ where: { githubUserId } })
}
我已经尝试在我的 return 语句中添加非空断言检查,但错误并没有消失。我唯一的解决方案是 "strict": false in tsconfig.json?
findOne()
的 return 类型是 User | null
(有一些条件,见下文)。该联合类型与函数 getPrismaUser()
的 return 类型不匹配,即 User
.
要修复您的错误,请将 getPrismaUser()
的 return 类型更改为 User | null
。
findOne returns a plain old JavaScript object or null.
The type of the object that's returned by a findOne API call depends on whether you use the select and include options.
If you use neither of these options, the return type will correspond to the TypeScript type that's generated for the model.
来自Prisma docs.