Enwrap 라이브러리
사용 편의성과 개발자 경험에 초점을 맞춰 함수를 래핑하고 유형 오류를 반환할 수 있는 작고(압축된 크기는 423바이트) 종속성이 없는 라이브러리입니다.
-------------------
import { ew } from 'enwrap'
// notice the first argument, `err`, this is a function we will call whenever
// we want to return an error
// any other arguments are the arguments we want to pass to the function
// in this case, we want to pass a number to the function
const getPositiveNumber = ew((err, num: number) => {
if (num < 0) {
return err('number must be positive')
}
return num
})
const res = getPositiveNumber(1)
// ^? `WithNoError<number> | TypedError<NonEmptyString, true> | TypedError<'number must be positive'>`
// if we want to access the number, we need to check if the error is present
if (res.error) {
console.log(res.error)
} else {
console.log(res) // 1
}
By 웹학교