Get keys of a Typescript interface as array of strings

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

    Instead of defining IMyTable as in interface, try defining it as a class. In typescript you can use a class like an interface.

    So for your example, define/generate your class like this:

    export class IMyTable {
        constructor(
            public id = '',
            public title = '',
            public createdAt: Date = null,
            public isDeleted = false
        )
    }
    

    Use it as an interface:

    export class SomeTable implements IMyTable {
        ...
    }
    

    Get keys:

    const keys = Object.keys(new IMyTable());
    

提交回复
热议问题