如何用映射类型表达 "At least one of the exisiting properties AND no additional properties"

How to express "At least one of the exisiting properties AND no additional properties" with a mapped type

标题略长请见谅

给定以下类型

type A = {
    foo: string;
    bar: number;
    baz: boolean;
}

我喜欢创建一个新的“部分”类型 B

type B = Partial<A>

这样 B 必须至少包含 A 的一个属性,并且只允许 A 的属性

//compiles
const b1 = {
   foo: "yeah"
}

//errors
const b2 = {}
const b3 = {lala: "lala"}
const b4 = {foo: "foo is alowed", but_not_this: false}
type A = {
  foo: string;
  bar: number;
  baz: boolean;

}
type AtLeastOne<Obj, Keys = keyof Obj> = Keys extends keyof Obj ? Pick<Obj, Keys> : never

type NonEmpty<T> = Partial<T> & AtLeastOne<T>

// Partial<A> & (Pick<A, "foo"> | Pick<A, "bar"> | Pick<A, "baz">)
type Result = NonEmpty<A>

//compiles
const b1: Result = {
  foo: "yeah"
}

//errors
const b2: Result = {}
const b3: Result = { lala: "lala" }
const b4: Result = { foo: "foo is alowed", but_not_this: false }

Playground

说明

Partial<A> & Pick<A, "foo"> | Pick<A, "bar"> | Pick<A, "baz"> 这是您最终应该达到的最低要求类型。

首先我们需要确保对象不为空。它应该具有三个道具之一。

考虑distributive-conditional-types

type AtLeastOne<Obj, Keys = keyof Obj> = Keys extends keyof Obj ? Pick<Obj, Keys> : never

根据文档,Pick<Obj, Keys> - 将应用于每个键。因此,AtLeastOne returns Pick<A, "foo"> | Pick<A, "bar"> | Pick<A, "baz">.

现在最简单的部分,你只需要使用交集来合并 return 类型的 AtLeastOnePartial<A>

type NonEmpty<T> = Partial<T> & AtLeastOne<T>

你会在我的打字稿中找到更多有趣的例子blog