To create a function with specific parameters, you can use the following syntax:
function someCustomFunction<
T,
K extends (keyof T)[]
>(object: T, propertiesAndFormat: {
properties: [...K],
format: (...arguments: {
[index in keyof K]: T[K[index]]
}) => string
}) {
return null!
}
The function has two generic parameters referred to as T
and K
. The T
parameter indicates the type of the object passed as object
, while K
represents a tuple of keys from T
. When specifying the type for the properties
parameter, the variadic tuple notation [...K]
is used to recognize K
as a tuple rather than an array due to the significance of order.
Regarding the format
parameter, it is defined as a rest parameter where the order and type of elements are determined by a tuple. This tuple is created by iterating over the elements denoted by I
in the tuple
K</code and retrieving the corresponding type of <code>T
.
Playground