Dhananjay Patel Logo
  1. Home
  2. / Blog
  3. / Typescript Basic
  4. / Lessons
  5. / 5

1. function takes input as function

index.ts
export type MutateFunction = (v: number) => number
const arrayMutate = (numbers: number[], mutate: MutateFunction) => {
return numbers.map(mutate)
}
// On other file we import `MutateFunction` type
const myMutateFunction: MutateFunction = (v: number) => v*10
console.log(arrayMutate([2,3,4], myMutateFunction))

2. function returns as function

index.ts
export type AdderFunction = (val: number) => number
const createAdder = (num: number): AdderFunction => {
return (val: number) => num + val
}
// On other file we import `AdderFunction` type
const addTwo: AdderFunction = createAdder(2)
console.log(addTwo(5))
console.log(addTwo(10))