Defining Function Types
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 28 of 73.
TypeScript allows you to create a type alias for a function's signature, which acts like a blueprint that functions must match.
A function type alias defines the shape of a function using the type keyword. The syntax shows the parameter types in parentheses followed by an arrow and the return type:
type MathOperation = (a: number, b: number) => number;
const add: MathOperation = (a, b) => {
return a + b;
};
const multiply: MathOperation = (a, b) => {
return a * b;
};In this example, MathOperation is a type alias that describes any function taking two numbers and returning a number. Both add and multiply conform to this type, ensuring consistency across your codebase.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a type alias named StringProcessor for a function that takes a single string parameter and returns a string.
Create a type alias named NumberCalculator for a function that takes two number parameters and returns a number.
Create a type alias named BooleanChecker for a function that takes a string parameter and returns a boolean.
Now implement the following functions that conform to these type aliases:
- Create a function named
toUpperCaseof typeStringProcessorthat converts the input string to uppercase - Create a function named
addPrefixof typeStringProcessorthat adds the prefix"Processed: "to the input string - Create a function named
divideof typeNumberCalculatorthat divides the first number by the second number - Create a function named
powerof typeNumberCalculatorthat raises the first number to the power of the second number (useMath.pow) - Create a function named
isEmptyof typeBooleanCheckerthat returnstrueif the string has length 0, otherwisefalse - Create a function named
startsWithAof typeBooleanCheckerthat returnstrueif the string starts with the letter "A" (case-sensitive), otherwisefalse
Test your functions by calling them with the following values and printing the results:
- Call
toUpperCasewith"hello world" - Call
addPrefixwith"data" - Call
dividewith20and4 - Call
powerwith3and4 - Call
isEmptywith"" - Call
startsWithAwith"Apple"
Print each result on a separate line in the order specified above.
Cheat sheet
TypeScript allows you to create a type alias for a function's signature using the type keyword. The syntax shows parameter types in parentheses followed by an arrow and the return type:
type MathOperation = (a: number, b: number) => number;
const add: MathOperation = (a, b) => {
return a + b;
};
const multiply: MathOperation = (a, b) => {
return a * b;
};Function type aliases act like blueprints that functions must match, ensuring consistency across your codebase.
Try it yourself
// TODO: Create type aliases here
// StringProcessor, NumberCalculator, BooleanChecker
// TODO: Implement the functions here
// toUpperCase, addPrefix, divide, power, isEmpty, startsWithA
// TODO: Test your functions and print the results
// Call each function with the specified values and print the resultsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples