Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS Is it possible to loop through keys of a type?

Tags:

typescript

Is it possible to loop over a type definition?

A simple type like:

interface Budget {
      bills: number;
      transportation: number;
}

And then something like:

Object.keys(Budget).map((item) => etc...)

Obviously the above doesn't work, is something like this possible? So that I don't have to create an object of type Budget whenever I want to utilize built in functions?

like image 207
Mathew Avatar asked Aug 31 '25 01:08

Mathew


1 Answers

The answer is no. Types are removed by the transpiler before runtime. In your case, the Budget reference will be undefined as no object named Budget exists in the transpiled code which is actually executed.

like image 99
Gershom Maes Avatar answered Sep 02 '25 14:09

Gershom Maes