Here is my answer, based on the question interpreted as in:
How to iterate during runtime on the enumeration of the (public) properties of a TypeScript declared class or other declared interface (or even object-type) ?
And the question could be varied into using private class members, given they would be accessible in either scope.
Answer: can't do. Unfortunately. The only thing you can do to get close to it is the following. And this will hopefully explain the ambiguity between the Q/As here, and what some TS devs might really want to do:
(Check/run this code below on the TS playground here)
interface IFoo {
firstProp: number
secondProp: number
}
class Foo implements IFoo {
readonly firstProp = 100;
readonly secondProp = 200;
someOtherProp = "bar";
}
enum IFooProps {
firstProp,
secondProp
}
for (key in Object.keys(IFooProps)) { // or at best IFoo, not needing to use an enum
// ... do something with interface keys
}
Note there isn't any way to check at compile-time so far IFooProps
and IFoo
actually match (TS feature request...).
And also, the above doesn't work so well, because keys of the enum object also includes its values ...
However, now to fix that, we could do:
const foo = new Foo();
Object.keys(IFooProps).forEach(prop => {
if (!isNaN(Number(prop))) return;
console.log(foo[prop]) // prints value of all IFoo implementations
});
// OR (this is just a "nicer" way to effectively run the same code as above here ^)
// can only be done with "runtime alive" objects (enums are, but class/interface are types not objects,
// but enums are also types too ;))
function enumKeys<O extends object, K extends keyof O = keyof O>(obj: O): K[] {
return Object.keys(obj).filter(k => Number.isNaN(+k)) as K[];
}
enumKeys(IFooProps).forEach(prop => {
console.log(foo[prop])
})
If TypeScript would allow to check that IFooProps
actually enumerates the properties of IFoo
(or the compiler would allow to declare/generate such an enum from any declared interface, explicitly or implicitly), then this would actually be type-safe (and convenient).
At the moment, this is rather a clumsy workaround, needing to declare the interface "twice" (redundantly in the enum).
Some credit to: https://www.petermorlion.com/iterating-a-typescript-enum/
(For helping out on the enumKeys
helper)
EDIT:
Another possible interpretation of this question (or to make sure to clarify with respect to what we said above):
Using enumerations of interface keys at compile-time (only)
Yes it is possible to enumerate on interfaces, BUT not at runtime:
(run this in TS playground here)
interface IBar {
firstThing: [string, string]
secondThing: [number, number]
}
function doThatWithTypedThing<Key extends keyof IBar>(someDeclaredThingKey: Key, someDeclaredThing: IBar[Key]) {
console.log(someDeclaredThingKey, someDeclaredThing)
}
const bar: IBar = {
firstThing: ["bli", "bla"],
secondThing: [400, 500]
}
doThatWithTypedThing("firstThing", bar.firstThing) // prints: firstThing [ "bli", "bla" ]
doThatWithTypedThing("secondThing", bar.secondThing) // prints: secondThing [ 400, 500 ]
doThatWithTypedThing("firstThing", bar.secondThing) // <- compiler error 2345
doThatWithTypedThing("notFirstThing", bar.secondThing) // <- other compiler error 2345