How to use TypeScript to point to a function
In TypeScript, creating functions is a fundamental aspect when explaining procedures and maintaining code
Multiple techniques are available for referencing functions, including creating a reference variable. This variable targets a particular function or establishes a type that describes the function's structure and uses it to define variable types.
Function referencing using a variable
We can use function references that enable us to call or invoke the function using the assigned variable, offering convenience in referencing and utilizing functions in TypeScript. It removes the need to rewrite the function definition repeatedly.
function greet(name: string) {console.log("Hello, " + name + "!");}let sayHello = greet;sayHello("John"); // Output: Hello, John!sayHello("Bob!"); // Output: Hello, Bob
Lines 1–3: The
greetfunction accepts anameparameter and logs a greeting message with the provided name.Lines 4–6: The
sayHellovariable is assigned as the reference to thegreetfunction. WhensayHellois called with the argument"John", it invokes the referenced function and outputs "Hello, John!" to the console. Similarly, it works for"Bob".
Function referencing by specifying function types
This approach helps to define a type that describes the structure of a function and then utilize that type to specify the variable's type.
type MathOperationType = (a: number, b: number) => number;function add(a: number, b: number): number {return a + b;}function subtract(a: number, b: number): number {return a - b;}let operation: MathOperationType;// Assign the add function to the operation variableoperation = add;const result = operation(3, 4);console.log("Result (add):",result); // Output: 7// Assign the subtract function to the operation variableoperation = subtract;const result2 = operation(8, 5);console.log("Result (subtract):",result2); // Output: 3
Line 1: Defines a type called
MathOperationTypeusing a function type annotation. It represents a function that takes twonumberarguments and returns anumber.Lines 3–9: We have a function named
addthat accepts twonumberargumentsaandb, and returns their sum. Similarly, another function namedsubtractis declared that takes twonumberargumentsaandb, and returns their difference.Lines 14–15: A variable named
operationof typeMathOperationTypeis declared but not yet assigned a function.Next, theoperationvariable is assigned theaddfunction, which matches theMathOperationTypefunction signature.Lines 16–17: The
operationvariable is invoked with arguments3and4, which calls the assignedaddfunction. The result7is stored in theresultvariable and printed to the console.Lines 19–21: Continuing the process, the
operationvariable is now assigned to thesubtractfunction. Subsequently, the function is invoked with the provided arguments, and the resulting values are stored and displayed in the console.
Conclusion
We can point to functions in TypeScript which can be achieved through function references or types, allowing for flexibility and re-usability in code. These approaches enable referencing and invocation of functions based on specific requirements.
Free Resources