Say I have a type like React.ComponentClass<Props>
and I want to reference the Props
part but it is unnamed in my current context.
To illustrate:
const x = makeComponent(); // typeof x = React.ComponentClass<Something>
In this situation, I can use typeof x
but that doesn't give me direct
access to the Something
type.
What I want is something like this:
type GetPropsType<C extends React.ComponentClass<P>> = P
So that I can extract Something
with the type GetPropsType<typeof x>
Anything equivalent would be great.
Assigning Generic ParametersBy passing in the type with the <number> code, you are explicitly letting TypeScript know that you want the generic type parameter T of the identity function to be of type number . This will enforce the number type as the argument and the return value.
Generics allow creating 'type variables' which can be used to create classes, functions & type aliases that don't need to explicitly define the types that they use. Generics makes it easier to write reusable code.
Generic MethodsA type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.
TypeScript supports generic classes. The generic type parameter is specified in angle brackets after the name of the class. A generic class can have generic fields (member variables) or methods. In the above example, we created a generic class named KeyValuePair with a type variable in the angle brackets <T, U> .
You can use infer:
type TypeWithGeneric<T> = T[]
type extractGeneric<Type> = Type extends TypeWithGeneric<infer X> ? X : never
type extracted = extractGeneric<TypeWithGeneric<number>>
// extracted === number
Playground
You can use pattern matching for that:
namespace React {
export class ComponentClass<T> {}
}
function doSomethingWithProps<T>(x : React.ComponentClass<T>) : T {
return null as T;
}
class Something {}
let comp = new React.ComponentClass<Something>();
const instanceOfSomething = doSomethingWithProps(comp);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With