Enwrap 라이브러리

· 8개월 전 · 384

사용 편의성과 개발자 경험에 초점을 맞춰 함수를 래핑하고 유형 오류를 반환할 수 있는 작고(압축된 크기는 423바이트) 종속성이 없는 라이브러리입니다.

 

https://github.com/biw/enwrap

 

-------------------

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
}

 

웹학교 무료강좌 / 웹학교 컨텐츠몰

PHP포탈 / 구인구직 솔루션

By 웹학교

|
댓글을 작성하시려면 로그인이 필요합니다.

365ok IT뉴스

+
제목 글쓴이 날짜 조회
8개월 전 조회 395
8개월 전 조회 445
8개월 전 조회 447
8개월 전 조회 421
8개월 전 조회 376
8개월 전 조회 485
8개월 전 조회 418
8개월 전 조회 363
8개월 전 조회 365
8개월 전 조회 396
8개월 전 조회 367
8개월 전 조회 425
8개월 전 조회 470
8개월 전 조회 374
8개월 전 조회 385
8개월 전 조회 373
8개월 전 조회 408
8개월 전 조회 413
8개월 전 조회 410
8개월 전 조회 430
8개월 전 조회 448
8개월 전 조회 423
8개월 전 조회 427
8개월 전 조회 376
9개월 전 조회 450
9개월 전 조회 373
9개월 전 조회 443
9개월 전 조회 467
9개월 전 조회 504
9개월 전 조회 480