我正在尝试创建一个可以拥有的界面
export interface MenuItem {
title: string;
component?: any;
click?: any;
icon: string;
}
Run Code Online (Sandbox Code Playgroud)
component或click设置我们的结构如下:
export type LinkRestSource = {
model: string;
rel?: string;
title?: string;
} | {
model?: string;
rel: string;
title?: string;
} | {
model?: string;
rel?: string;
title: string;
};
Run Code Online (Sandbox Code Playgroud)
这几乎与说法相同
type LinkRestSource = Partial<{model: string, rel: string, title: string}>
Run Code Online (Sandbox Code Playgroud)
除了这将允许传入空对象,而初始类型需要传递其中一个属性
我怎样才能创建类似的泛型Partial,但其行为与上面的结构相似?
我有两种类型的联合,其中一种是空的 obj。
type U = {} | { a: number } // | { b: string } | { c: boolean } ....
Run Code Online (Sandbox Code Playgroud)
我想从联合中排除空对象但是Exclude没有帮助
type A = Exclude<U, {}>
// A = never
Run Code Online (Sandbox Code Playgroud)
我尝试使用,as const但结果相同
const empty = {} as const
type Empty = typeof empty
type U = Empty | { a: number }
type A = Exclude<U, Empty>
//type A = never
Run Code Online (Sandbox Code Playgroud)
额外的讽刺是,排除其他属性很简单
type B = Exclude<U, { a: number }>
// type B …Run Code Online (Sandbox Code Playgroud)