1 | initial version |
The TS function parameter type that is determined by a different parameter is called a conditional type. It allows you to specify a type for a parameter based on another parameter or condition. This is done using the conditional operator (? :
) within the type definition. For example:
type MyFunction<T, U> = T extends string ? (arg: U) => string : (arg: U) => number;
In this example, the MyFunction
type takes two parameters T
and U
. If T
is string
, the function returns a string; if not, it returns a number. The type of the function's parameter arg
is determined by the type of U
. This is useful for creating generic types that have different behavior based on their input types.