Why to use intersection with empty object type and mapped type in TypeScipt

Viewed 175

In the source code of ts-toolbelt, there is often an intersection of an empty object and a mapped type. For what purpose is this done?

export type ReadonlyFlat<O> = {
  +readonly [K in keyof O]: O[K];
} & {}; // there

export type OptionalFlat<O> = {
  [K in keyof O]?: O[K];
} & {}; // there

type __Pick<O extends object, K extends keyof O> = {
  [P in K]: O[P];
} & {}; // there
0 Answers
Related