首页 > 其他分享 >[Typescript] 136. Medium - Object to Union

[Typescript] 136. Medium - Object to Union

时间:2022-12-14 15:46:04浏览次数:64  
标签:Typescript string Union Object keyof 136 type

import { Equal, Expect } from "../helpers/type-utils";

interface Attributes {
  id: string;
  email: string;
  username: string;
}

/**
 * How do we create a type helper that represents a union
 * of all possible combinations of Attributes?
 */
type ObjectToUnion<T> = {
  [Key in keyof T]: Record<Key, T[Key]>;
}[keyof T];

type ExclusiveAttributes = ObjectToUnion<Attributes>;

type tests = [
  Expect<
    Equal<
      ExclusiveAttributes,
      | {
          id: string;
        }
      | {
          email: string;
        }
      | {
          username: string;
        }
    >
  >
];

 

标签:Typescript,string,Union,Object,keyof,136,type
From: https://www.cnblogs.com/Answer1215/p/16982343.html

相关文章