postgresql 类型< variable>不满足约束“never”,ts(2344)

6uxekuva  于 2022-12-26  发布在  PostgreSQL
关注(0)|答案(2)|浏览(152)

So i'm trying to use Supabase + typescript but when i want to use insert(), update(), upsert() and other functions im getting an error when i want to declare the object that i want to declare "Type does not satisfy the constraint 'never'. ts(2344)"
这是我的代码:

type Restaurant = {
    id: string;
    name: string;
    categories: string[];
    open: boolean;
    description: string;
  }

const addRestaurant = async () => {
    if (!user.value) return 
    
    const { data, error } = await supabaseClient
      .from('restaurants')
      .upsert<Restaurant>({
        id: user.value.id,
        name: name.value,
        categories: arr,
        open: status.value,
        description: description.value
      })
      .select()
      console.log(data, error);

  }

im getting that error on but if i don't specify the type of it i get almost the same error but saying:
类型为"{id:字符串;名称:字符串;类别:字符串;打开:布尔值;说明:string ;}"不能赋给类型" never []"的参数。对象文本只能指定已知属性,并且类型" never []"中不存在" id "。ts(2345)
另外,当我将鼠标悬停在"upsert()"函数上时,它看起来像是在后台使用此函数
第一个月
我该怎么补救呢?

  • 我试着指定对象的类型,但是函数[insert,update,upsert]似乎要求其他的东西。
  • 我发现,如果我添加第二个参数与一个空字符串的错误消失,但如果我编辑它里面的东西,我得到的错误再次(这不应该是最好的方式来修复它)
  • 代码可以在有或没有第二个参数的情况下工作,这只是我在开发过程中遇到的错误-而且我不想关闭Typescript的属性,因为我想看到什么时候出现错误
shstlldc

shstlldc1#

要解决这个问题,我所要做的就是从Supabase生成类型,我使用Supabase CLI并在控制台上运行以下命令:

supabase gen types typescript --project-id

然后我不得不像这样把类型注入到我的supabase客户端:

import { createClient } from '@supabase/supabase-js'
import { Database } from 'lib/database.types'

const supabase = createClient<Database>(
  process.env.SUPABASE_URL,
  process.env.SUPABASE_ANON_KEY
)
zf9nrax1

zf9nrax12#

对于supabase-js,这不是你处理类型的方式。请阅读文档的TypeScript support部分以更好地理解这一点。如果你不指定类型,除非你传入的值是never[]类型,否则应该不会出错。在你的代码中,user.value.id是不确定的,所以你得到的错误是正确的,因为你没有防范条款。

相关问题