Is there a way to pass a string parameter to a function and retrieve an object with that key?
function getFunction(name: string): { [name]: () => number } {
return {
[name]: () => {
console.log(1);
return 2;
},
};
}
const { myName } = getFunction("myName");
I am interested in using TypeScript to generate hooks by endpoints property in rtk query createApi.
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import type { Pokemon } from './types'
// Defining a service with a base URL and expected endpoints
export const pokemonApi = createApi({
reducerPath: 'pokemonApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getPokemonByName: builder.query<Pokemon, string>({
query: (name) => `pokemon/${name}`,
}),
}),
})
// Exporting hooks for functional components, which are
// automatically generated based on the defined endpoints
export const { useGetPokemonByNameQuery } = pokemonApi