utils/src/types.ts

21 lines
425 B
TypeScript
Raw Normal View History

2024-09-22 02:38:13 -04:00
/**
* Return keys on a type as an array of strings
*
* @example
* ```ts
* type Person = {
* firstName: string;
* lastName: string;
* age: number;
* }
*
* const keys = typeKeys<Person>();
* console.log(keys); // Output: ["firstName", "lastName", "age"]
* ```
*
* @return {Array<keyof T>} Available keys
*/
2024-11-07 10:46:56 -05:00
export function typeKeys<T extends object>() {
2024-09-22 02:38:13 -04:00
return Object.keys(<T>{}) as Array<keyof T>;
}