Get keys of a Typescript interface as array of strings

后端 未结 12 1747
攒了一身酷
攒了一身酷 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:34

    As of TypeScript 2.3 (or should I say 2.4, as in 2.3 this feature contains a bug which has been fixed in typescript@2.4-dev), you can create a custom transformer to achieve what you want to do.

    Actually, I have already created such a custom transformer, which enables the following.

    https://github.com/kimamula/ts-transformer-keys

    import { keys } from 'ts-transformer-keys';
    
    interface Props {
      id: string;
      name: string;
      age: number;
    }
    const keysOfProps = keys();
    
    console.log(keysOfProps); // ['id', 'name', 'age']
    

    Unfortunately, custom transformers are currently not so easy to use. You have to use them with the TypeScript transformation API instead of executing tsc command. There is an issue requesting a plugin support for custom transformers.

提交回复
热议问题