Is it possible to use the type of a property of an Interface as a generic in TypeScript? I have some code that demonstrates what I'm trying to achieve:
In the example below, I show how we can normally define types using enums and interfaces:
enum Sections {
users = 'users',
projects = 'projects'
}
interface SectionEles {
[Section.users] : {...};
[Section.projects]: {...};
}
interface SezViewSettings<S extends Sections> = {
section: S;
where: Array<keyof SectionEles[S]>;
}
While the above code works fine, I am curious if it's possible to avoid making SezViewSettings
a generic. Instead, I would like to infer the type S
from the value assigned to the property section
, similar to this:
interface SezViewSettings = {
section: S extends Sections;
where: Array<keyof SectionEles[S]>;
}
Do you think this is achievable?