Jan 3, 2024
- Template String
Get the return type of the function
Get the parameters type list
Note: For above two examples they showed the keyword infer
's usage.
Namely you can treat them as a variable that contain the correspond type.
For example, in type Parameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never
, the infer P
means storing the types of ...args
into P
and assigns P
's value to the Parameters
if T
is an function.
Restrict the variable equals to one of the elements in an Tuple
Note: In this example the myTuple
was perceived an readonly tuple and every element are its literal type, namely the first element is the 'a'
type rather then string
. If you want to make MyType = string | number | boolean
you can remove the as const
assertion.