How do I modify the key of a function returned object to match its parameter's value?
Here is my current code:
const createCache = <A, T extends string>(name: T, base = {}) => {
const cache = base;
return {
[name]: cache,
} as {
[key in keyof T]: typeof cache; // this didn't work
// [key in typeof name]: typeof cache; // this didn't work
// [key in keyof typeof name]: typeof cache; // this didn't work
// [key: typeof name]: typeof cache; // this didn't work
};
};
I am attempting to implement an autocomplete feature with the returned value matching the key:
createCache('root').root // okay
createCache('hello').hello // okay
makecache('error').notError // error!
I have successfully achieved this before, but I cannot recall how.