I'd like to have an interface generated out of the values of an enum. I have the following use-case in React:
I have an enum with potentially a lot of key value pairs. Each of the enum values is used as form IDs, so I get the name and the value of the input element in an event listener. I'd like to set the state to this.setState({ name: value }), but the name, description, etc. should be type-safe.
So I somehow need to generate an interface out of the values of the enum (because an interface cannot inherit from an enum) to be able to do the following for example: this.setState({ name: 'hello world' }) and this.setState({ description: 'a description' })
enum InputIDs {
NAME = 'name',
DESCRIPTION = 'description',
// ... many more items ...
}
interface IMyReactComponentState {
alreadyExisting: boolean;
[InputIDs.NAME]: string;
// ... all other items from the Test enum should go here but I'd like to generate it somehow ...
}
class MyReactComponent extends React.Component< ISomeProps, IMyReactComponentState > {
constructor(props: ISomeProps) {
super(props);
this.state = {
alreadyExisting: false,
[InputIDs.NAME]: '',
// more default values
}
}
private handleChange = (event: React.FormEvent<HTMLDivElement>) => {
// TODO make type safe
const {name, value}: {name: any, value: string} = (event.target as any); // event.target is either HTMLInputElement, HTMLSelectElement or HTMLTextAreaElement
// store the value of the corresponding input in state to preserve it when changing tabs
this.setState({
[name]: value
});
}
}
My problem is that something along these lines is not possible:
interface IMyReactComponentState extends InputIDs {
alreadyExisting: boolean;
}
Any ideas how I can keep the enum with the typings of IMyReactComponentState in sync without writing an interface myself?
Thanks in advance! Not sure if this has been asked already - if so I haven't found the answer yet!
EDIT (May 8th, 2019):
We're using TypeScript 2.8.1 in our project