相关疑难解决方法(0)

typescript接口需要存在两个属性之一

我正在尝试创建一个可以拥有的界面

export interface MenuItem {
  title: string;
  component?: any;
  click?: any;
  icon: string;
}
Run Code Online (Sandbox Code Playgroud)
  1. 有没有办法要求componentclick设置
  2. 有没有办法要求两个属性都不能设置?

typescript

48
推荐指数
8
解决办法
2万
查看次数

如何创建类似于Partial的,需要设置单个属性

我们的结构如下:

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,但其行为与上面的结构相似?

generics typescript

16
推荐指数
4
解决办法
4051
查看次数

是否可以从联合中排除空对象?

我有两种类型的联合,其中一种是空的 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)

typescript typescript3.0

5
推荐指数
1
解决办法
165
查看次数

标签 统计

typescript ×3

generics ×1

typescript3.0 ×1