In this lesson you will learn how to use TypeScript to describe how functions behave, and apply these ideas to designing reusable library code.
You will work in pairs throughout the lesson. Expect to discuss, write code, and compare ideas.
By the end of this lesson you should be able to:
- Add type annotations to functions
- Read and explain function types
- Use generics to write reusable functions
- Design a clear, typed function API
Decide whether each function is pure or impure.
function square(n) {
return n * n
}let counter = 0
function increment() {
counter++
}function doubleAll(arr) {
return arr.map(n => n * 2)
}function randomNumber() {
return Math.random()
}- Which of these would you trust in a library?
- Why?
Be ready to share.
function filterBy<T>(arr: T[], fn: (item: T) => boolean): T[]- Write your answers down
- Be ready to explain
Answer:
- What does this function do?
- What does
fndo? - What is
T?
We will discuss answers as a class.
function double(n: number): number {
return n * 2
}
const fn: (n: number) => number = double(n: number) => number- takes a number
- returns a number
Work with a partner.
- Write your answers down
- Be ready to explain your answers
function applyTwice(value: number, fn: (n: number) => number): numberDiscuss and write:
- What does
fntake? - What does it return?
- What does
applyTwicereturn?
Work with a partner.
- Write your answers down
- Be ready to explain your reasoning
function mapNumbers(arr: number[], fn: (n: number) => number): number[]Discuss and write:
- What does the function
fntake? - What does it return?
- What does
mapNumbersreturn?
Work with a partner.
- Write your answers down
- Be ready to explain your reasoning
function checkAll(arr: number[], fn: (n: number) => boolean): booleanDiscuss and write:
- What does the function
fntake? - What does it return?
- What does
checkAllreturn?
function firstNumber(arr: number[]): number
function firstString(arr: string[]): stringWhat is wrong with this approach?
function first<T>(arr: T[]): T | undefined {
return arr[0]
}T is a placeholder type determined when the function is called.
Write:
function last<T>(arr: T[]): T | undefinedExplain:
- What is
T? - Where does it come from?
Convert these to TypeScript:
function unique(array) {
return [...new Set(array)]
}function sum(nums) {
return nums.reduce((acc, n) => acc + n, 0)
}function filter(arr, fn) {
return arr.filter(fn)
}Focus on:
- parameter types
- return types
- function types
Create a function that:
- filters items
- also limits the number of results
Create a function that:
- maps items
- callback receives (item, index)
Create a function that:
- extracts a property
- works with any key (not just "name")
Create a function that:
- groups items into categories
- returns an object of arrays
Write down:
- function name
- parameter names
- what the function should do
- one example call
- add TypeScript types
- use generics where needed
- type any callback functions
- update your example call if needed
Write at least two edge cases:
- empty array
- unexpected input
Join another group.
- Explain your function to them
- Compare designs
- Decide which version is clearer and why
Focus on:
- naming
- parameter order
- type clarity
- Accept the assignment: https://classroom.github.com/a/tpmlN-gN
- Clone your repo
- Run:
npm ci
npm run build
npm testIf any command fails, stop and ask for help immediately.
- Begin implementing functions
Answer:
- What does
(item: T) => booleanmean? - What does
Trepresent? - What makes a function API clear?