I am facing a scenario where I receive keys from the backend and need to design an interface based on these keys. By creating a dynamic interface, I can easily bind these properties.
const KEYS_FROM_API = ['ARE_YOU_SURE', 'NOT_NOW', 'GO_BACK'];
// defining type dynamically
type DYNAMIC_KEYS_TYPE = typeof KEYS_FROM_API[number];
// creating dynamic interface using Record
type ResultType = Record<DYNAMIC_KEYS_TYPE, string>
// RESULT stores key value pairs with the type of dynamic interface, ResultType
const RESULT: ResultType = { ARE_YOU_SURE: 'Are You sure', NOT_NOW: 'Not Now', GO_BACK: 'go back'}
// works as expected
console.log(RESULT.ARE_YOU_SURE, RESULT.NOT_NOW, RESULT.GO_BACK);
The provided code meets the expectations and there are no linting errors, as the RESULT constant has a type that includes all the properties from the interface.
Now, I am interested in creating the DYNAMIC_KEYS_TYPE dynamically using the KEYS_FROM_API array. Instead of hardcoding it like in the above example, I am looking for a way to generate this type based on the contents of the KEYS_FROM_API array.
I need assistance in creating the DYNAMIC_KEYS_TYPE from the KEYS_FROM_API array.