It seems like you want Props
to be generic so that it can be used by different object types. This can be achieved by defining a generic type T
in Props
interface Props<T> {
dropdownList: T[];
dropdownAttributes: (keyof T)[];
}
Now, if we know the types of a certain object in advance, we can create an interface for that, and create a type that uses that interface in Prop
interface MyDropDownItem {
foo : number
}
type MyDropDownItemProps = Props<MyDropDownItem>;
We can now only use instances of MyDropDownItem
in dropdownList
and its keys in dropdownAttributes
const good: MyDropDownItemProps = {
dropdownList: [{foo: 2}],
dropdownAttributes : ['foo']
}
const bad: MyDropDownItemProps = {
dropdownList: [{foo: 2, bar: 's' /* error here */}],
dropdownAttributes : ['foo', 'bar' /* and here */ ]
}
This of course assumes you know the structure of your dropdowns in advance, because that's the only thing typescript can help you with. Typescript won't help you with runtime type safety.
Check it out on stackblitz