我有以下由Suabase 生成的Typescript接口
export interface definitions {
Users: {
/** Format: uuid */
id: string;
/**
* Format: timestamp with time zone
* @default now()
*/
created_at?: string;
/**
* Format: uuid
* @description Note:
* This is a Primary Key.<pk/>
*/
};
Content: {
/**
* Format: uuid
* @description Note:
* This is a Primary Key.<pk/>
*/
id: string;
/**
* Format: timestamp with time zone
* @default now()
*/
created_at?: string;
/** Format: text */
name: string;
/** Format: text */
description: string;
};
}
此接口不能修改,因为它是自动生成的。
我通常在查询中这样使用它:
import { definitions } from "../types/supabase";
const id = 1
let { data, error } = await supabase
.from<definitions["Users"]>("Users").select("created_at")
.eq("id", id);
然而,对于这个查询,我需要扩展我的接口:
let { data: Content, error } = await supabase
.from<ContentAndUsers>("Content")
.select(`*, Users (id, created_at)`)
.eq("id", id);
我尝试创建接口,但它给了我TS错误:
interface UserContentCombo extends definitions["Content"] {
...definitions["Users"]
}
正确的语法是什么?感谢
如果您想要一个包含Users
和Content
的所有属性的类型,这可能是最短的方法:
type UserContentCombo = definitions["Content"] & definitions["Users"]
游乐场
我看到错误显示:
接口只能使用扩展标识符/限定名称可选类型参数
因此,要给它一个标识符并修复它,您可以定义一个中间类型:
type userDefinitions = definitions['User'];
然后使用该标识符:
interface UserContentCombo extends userDefinitions {}
无需使用排列运算符
TS游乐场