Typing Params & Return Values
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 22 of 73.
Functions are the building blocks of any application, and TypeScript makes them safer and more predictable by allowing you to specify exactly what types of data they accept and return. When you add type annotations to functions, you create a clear contract that defines what goes in and what comes out.
To add types to a function, you specify the type for each parameter after its name, and optionally specify the return type after the parameter list:
function greet(name: string): string {
return "Hello, " + name;
}
function multiply(a: number, b: number): number {
return a * b;
}The syntax follows a simple pattern: parameterName: type for each parameter, and : returnType after the closing parenthesis.
Challenge
EasyCreate a function named add that takes two parameters of type number and returns their sum. The function must have an explicit return type annotation of number.
Create another function named getFullName that takes two parameters: firstName of type string and lastName of type string. The function should return the full name as a single string with a space between the first and last names. Add an explicit return type annotation of string.
Create a third function named isEligible that takes two parameters: age of type number and hasLicense of type boolean. The function should return true if the person is 18 or older AND has a license, otherwise return false. Add an explicit return type annotation of boolean.
Test your functions by calling them with the following values and printing the results:
- Call
addwith15and27 - Call
getFullNamewith"John"and"Smith" - Call
isEligiblewith20andtrue - Call
isEligiblewith16andtrue
Print each result on a separate line in the order specified above.
Try it yourself
// TODO: Write your code here
// Create the add function with explicit return type annotation
// Create the getFullName function with explicit return type annotation
// Create the isEligible function with explicit return type annotation
// Test the functions and print the results
// Call add with 15 and 27
// Call getFullName with "John" and "Smith"
// Call isEligible with 20 and true
// Call isEligible with 16 and trueThis 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