.png)
📖 전예홍, ⌈Do it! 타입스크립트 프로그래밍⌋, 이지스퍼블리싱, 2021
export const init = (callback: () => void): void => {
console.log('default initialization finished.')
callback()
console.log('all initialization finished.')
}
import {init} from './init'
init(() => console.log('custom initialization finished.'))
// 실행 결과
// default initialization finished.
// custom initialization finished.
// all initialization finished.
export type NumbrToNumberFunc = (number) => number
export const add = (a: number): NumberToNumberFunc => {
const _add: NumberToNumberFunc = (b: number): number => {
return a + b // 클로저
}
return _add
}
import {NumberToNumberFunc, add} from './add'
let fn: NumberToNumberFunc = add(1)
let result = fn(2)
console.log(result) // 3
console.log(add(1)(2)) // 3
export type Person = {name: string, age: number}
export const makePerson = (name: string, age: number = 10): Person => {
const person = {name, age}
return person
}
console.log(makePerson('Iris')) // { name: 'Iris', age: 10 }
console.log(makePerson('Iris', 24)) // { name: 'Iris', age: 24 }
export type Person = {name: string, age: number}
export const makePerson = (name: string, age: number = 10): Person => ({name, age})
console.log(makePerson('Iris')) // { name: 'Iris', age: 10 }
console.log(makePerson('Iris', 24)) // { name: 'Iris', age: 24 }
export type KeyValueType = {
[key: string]: string
}
export const makeObject = (key: string, value: string): KeyValueType => ({[key]: value})
console.log(makeObject('name', 'Iris')) // { name: 'Iris' }
console.log(makeObject('firstName', 'Iris')) // { firstName: 'Iris' }
콜백 함수, 고차 함수 등 타입스크립트의 여러 함수 관련 문법들을 학습했다. 처음 읽어나가는 부분인 만큼 아직 이해가 잘 되지 않는 부분들이 있어 표시해둔 뒤 내일 복습하며 다시 한번 읽어보기로 했다.