Working with tabular data in Typescript requires the use of interfaces to define column structures. To efficiently manipulate these structures, it is often necessary to retrieve the property names of these interfaces as an array of strings.
Since Typescript version 2.4, custom transformers provide a mechanism to extract keys from interfaces. Consider the following interface:
<code class="typescript">interface IMyTable { id: number; title: string; createdAt: Date; isDeleted: boolean; }</code>
To obtain the property names as an array:
<code class="typescript">import { keys } from 'ts-transformer-keys'; const IMyTable = keys<IMyTable>(); console.log(IMyTable); // ["id", "title", "createdAt", "isDeleted"]</code>
While custom transformers offer a convenient solution, they require the use of the Typescript transformation API rather than the ts command. This limitation can hinder their usability.
In scenarios where custom transformers are not feasible, alternative options include:
The above is the detailed content of How to Retrieve the Keys of a TypeScript Interface as an Array of Strings?. For more information, please follow other related articles on the PHP Chinese website!