28
loading...
This website collects cookies to deliver better user experience
Generic functions
(aka General Functions) in TypeScript.generic functions
is to have a type safety by associating the type of the function parameters types (input) with the return value type (output) of the function.Generic functions
are those functions that set their type dynamically without us specifying it manually.// Logic of the function is to get the first element from the array
// The Function is also a Generic Function
// and type safe where it can accept an array
// of any type and returns the same type
// of first element dynamically without
// we defining the type manually
function getFirstElement<GeneralType>(arr: GeneralType[]): GeneralType {
return arr[0];
}
// call the function with array of numbers
const value1 = getFirstElement([1, 2, 3, 4]); // type => number 😍
// call the function with array of strings
const value2 = getFirstElement(["hello", "hai", "hey"]); // type => string 😍
// Logic of the function is to get the first element from the array
function getFirstElement(arr) {
return arr[0];
}
getFirstElement
function and pass a array of numbers like this,// Logic of the function is to get the first element from the array
function getFirstElement(arr) {
return arr[0];
}
// call the function
const value1 = getFirstElement([1, 2, 3, 4]); // type => any 😕
value1
variable, we can see that the type is of any
which is the expected behavior of the function since we haven't defined the parameter type as well as the return value type.arr
to hold an array of numbers
and the return value of the function be a number
.// Logic of the function is to get the first element from the array
function getFirstElement(arr: number[]): number {
return arr[0];
}
// call the function
const value1 = getFirstElement([1, 2, 3, 4]); // type => number 🙂
getFirstElement
function call we can see that the type is of number
which is good.string
type which we cannot do with our current function even though the mechanism or the logic we need is of the same.getFirstElement
function into a generic function
by using a Type
Parameter.Type
parameter is declared by writing out the general type name inside an angled bracket symbol (<>
) after the function name.GeneralType
.// Logic of the function is to get the first element from the array
function getFirstElement<GeneralType>(arr: number[]): number {
return arr[0];
}
// call the function
const value1 = getFirstElement([1, 2, 3, 4]); // type => number 🙂
Type
parameter.Type
parameter called GeneralType
wherever we have used the number
type.// Logic of the function is to get the first element from the array
// The Function is also a Generic Function
// and type safe where it can accept an array
// of any type and returns the same type
// of first element dynamically without
// we define the type manually
function getFirstElement<GeneralType>(arr: GeneralType[]): GeneralType {
return arr[0];
}
// call the function
const value1 = getFirstElement([1, 2, 3, 4]); // type => number 🙂
getFirstElement
function is now a Generic Function
and can now accept an array of any value type and returns the same type of the first element from the array dynamically.getFirstElement
with both array of numbers
and array of strings
and analyze the return types.// Logic of the function is to get the first element from the array
// The Function is also a Generic Function
// and type safe where it can accept an array
// of any type and returns the same type
// of first element dynamically without
// we define the type manually
function getFirstElement<GeneralType>(arr: GeneralType[]): GeneralType {
return arr[0];
}
// call the function with array of numbers
const value1 = getFirstElement([1, 2, 3, 4]); // type => number 😍
// call the function with array of strings
const value2 = getFirstElement(["hello", "hai", "hey"]); // type => string 😍
value1
and the value2
variables is according to the type of the array elements passed to the getFirstElement
function.Generic Function
.