gpt4 book ai didi

TypeScript: "Type is not assignable to type 'never'" with Conditional Types in Supabase Query(TypeScrip:在Supabase Query中使用条件类型时,不能将类型分配给类型‘Never’“)

转载 作者:bug小助手 更新时间:2023-10-26 20:33:50 33 4
gpt4 key购买 nike



Question


I am working on a Next.js project using Supabase for the backend. I have defined some helper types to handle the responses from Supabase queries, but I'm running into a TypeScript issue that I can't seem to resolve.

我正在做一个使用Supabase作为后端的Next.js项目。我已经定义了一些帮助器类型来处理来自Supabase查询的响应,但我遇到了一个似乎无法解决的打字问题。


Helper Types:


Here are my helper types and a brief description of each:

以下是我的帮助器类型以及每种帮助器的简要说明:



  • DbResult<T>: This type is intended to unwrap the type from a promise. If T is a promise, it infers the type U that the promise resolves to.

    DbResult :此类型用于从Promise中展开类型。如果T是承诺,则它推断承诺解析为的类型U。



  • DbResultOk<T>: This type is meant to handle successful query results. It infers the data field from the promise and excludes null values.

    DbResultOk :该类型用于处理成功的查询结果。它从Promise推断数据字段并排除空值。



  • DbResultErr: This type is simply an alias for PostgrestError, intended to handle errors from the Supabase query.

    DbResultErr:该类型只是PostgrestError的别名,用于处理来自Supabase查询的错误。




export type DbResult<T> = T extends PromiseLike<infer U> ? U : never;
export type DbResultOk<T> = T extends PromiseLike<{ data: infer U }> ? Exclude<U, null> : never;
export type DbResultErr = PostgrestError;

Code:


I am using these types in a function that performs a Supabase query:

我在执行Supabase查询的函数中使用这些类型:


export async function addFrequentIngresoAccion(payload: {
// ... payload definition
}): Promise<DbResult<any>> {
const supabase = createServerActionClient<Database>({ cookies });
const query = supabase.from("FuenteIngreso").insert([/*...*/]).select();
const result: DbResult<typeof query> = await query;

if (result.error) {
const resultErr: DbResultErr = result.error;
return { error: resultErr };
}

const resultOk: DbResultOk<Tables<"FuenteIngreso">[]> = result.data as Tables<"FuenteIngreso">[];
return { data: resultOk };
}

The line const resultOk: DbResultOk<Tables<"FuenteIngreso">[]> = result.data as Tables<"FuenteIngreso">[]; is throwing the following error:

行Const ResultOk:DbResultOk []>=Result.data as Tables<“FuenteIngreso”>[];引发以下错误:


Type '{ CantidadIngresoRecurrente: number | null; created_at: string; es_recurrente: boolean | null; fechadepositorecurrente: string | null; fuente_name: string; id: number; LastIngresoAdded: string | null; user_id: string; }[]' is not assignable to type 'never'.ts(2322)

I don't understand why the type is falling back to never. Can anyone help me understand what's going wrong here? Also, if my understanding of the helper types is incorrect, please feel free to correct me.

我不明白为什么这种类型会倒退到永远不会。有没有人能帮我弄明白这里出了什么问题?另外,如果我对帮手类型的理解不正确,请随时纠正我。


更多回答
优秀答案推荐

I literally just encountered this exact issue today... what are the odds. For me it was the return value of a Supabase Postgres Function and not a table but I think the change should be exactly what you need or close enough.

我今天确实遇到了这个问题……这几率有多大。对我来说,它是Supabase Postgres函数的返回值,而不是表,但我认为更改应该恰好是您需要的,或者足够接近。


Change this line:

更改此行:


const resultOk: DbResultOk<Tables<"FuenteIngreso">[]> = result.data as Tables<"FuenteIngreso">[];

to this:

对此:


const resultOk: DbResultOk<typeof query> = result.data

Why this change?

为什么会有这样的变化?


DbResultOk is defined as this:

DbResultOk的定义如下:


export type DbResultOk<T> = T extends PromiseLike<{ data: infer U }> ? Exclude<U, null> : never

It is similar to the DbResult definition:

它类似于DbResult定义:


export type DbResult<T> = T extends PromiseLike<infer U> ? U : never

Except that generic type T is referencing the data part of the query Result object. Long story short though, it must also be typeof query like DbResult because T must extend PromiseLike<>.

只是泛型类型T引用的是查询结果对象的数据部分。长话短说,它还必须是像DbResult这样的查询类型,因为T必须扩展PromiseLike<>。


This is part of a larger concept in TS called Conditional Types, and TS documentation about this can be found here:
https://www.typescriptlang.org/docs/handbook/2/conditional-types.html

这是TS中称为条件类型的更大概念的一部分,有关这一概念的TS文档可以在以下位置找到:https://www.typescriptlang.org/docs/handbook/2/conditional-types.html


更多回答

Hi @roofer_chill, Thank you for your insightful answer! Changing the line to const resultOk: DbResultOk<typeof query> = result.data did indeed resolve one of the issues I was facing. I'm still pondering over the appropriate return type for my server action. I'm considering using a conditional type like so: typescript type ServerActionReturnType<T> = { data: DbResultOk<T> } | { error: DbResultErr }; And then having my server action return a Promise<ServerActionReturnType<any>>. Would you say this is a good approach? Thanks again for your help!

Hi@Roofer_Chill,感谢您富有洞察力的回答!将行更改为Const ResultOk:DbResultOk=Result.data确实解决了我面临的一个问题。我仍然在考虑为我的服务器操作选择合适的返回类型。我正在考虑使用这样的条件类型:类型脚本类型ServerActionReturnType={data:DbResultOk}|{Error:DbResultErr};然后让我的服务器操作返回承诺>。你认为这是一个好方法吗?再次感谢您的帮助!

Honestly I'm not the best at error handling architecture, but I think it would be better to throw the DbResultErr and handle them further up the application. With that, you could just return the DbResultOk type as you are throwing the errors.

老实说,我并不擅长错误处理体系结构,但我认为抛出DbResultErr并在应用程序中进一步处理它们会更好。这样,您就可以在抛出错误时返回DbResultOk类型。

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com