Get keys of a Typescript interface as array of strings

后端 未结 12 1749
攒了一身酷
攒了一身酷 2020-11-28 03:05

I\'ve a lot of tables in Lovefield and their respective Interfaces for what columns they have.
Example:

export interface IMyTable {
  id: number;
  t         


        
12条回答
  •  独厮守ぢ
    2020-11-28 03:42

    The following requires you to list the keys on your own, but at least TypeScript will enforce IUserProfile and IUserProfileKeys have the exact same keys (Required was added in TypeScript 2.8):

    export interface IUserProfile  {
      id: string;
      name: string;
    };
    type KeysEnum = { [P in keyof Required]: true };
    const IUserProfileKeys: KeysEnum = {
      id: true,
      name: true,
    };
    

提交回复
热议问题