V
V
Vann Damm2021-02-17 16:43:41
typescript
Vann Damm, 2021-02-17 16:43:41

How to dynamically pass a generic type?

there is an interface

export interface AuthFormsDataTypes {
  login: LogInFormDataType;
  registration: RegFormDataType;
}

I need to dynamically extract the type login and registration.ps commonName from it - a string. How can I do that?
onSubmit: submitHandlersCreator<AuthFormsDataTypes[commonName]>({
                  dispatch,
                  path: `${location.pathname}${location.search}`,
                  actionName:commonName
                }),

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-02-17
@effect_tw

If commonName is a variable, then its type can be retrieved via typeof

submitHandlersCreator<AuthFormsDataTypes[typeof commonName]>({
                  dispatch,
                  path: `${location.pathname}${location.search}`,
                  actionName:commonName
                })
But it must have a literal type or a union of literal types, moreover, from the set of keys AuthFormsDataTypes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question