How do I declare generic type for a function created dynamically?
type FooType = {
username: string;
}
type BarType = {
age: number;
}
interface DataStore {
foo: FooType;
bar: BarType;
}
const getDataStore = () => ({
foo: { username: 'Tera' },
bar: { age: 24 },
})
const generateFunction = <T, S extends keyof DataStore>(slice: S) => {
type DataSlice = DataStore[S];
return (selector: (store: DataSlice) => T) => selector(getDataStore()?.[slice]);
}
How do I use T
in useFoo
and pass generateFunction
?
const useFoo = generateFunction('foo');
Expected usage of useFoo
type UserName = string;
const userName = useFoo<UserName>((foo: FooType) => foo.userName);