1.0.2 • Published 4 years ago

@maboroshi/type-assertions v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

type-assertions

型アサーションユーティリティ
https://github.com/maboroshi-inc/type-assertions

インストール

npm install @maboroshi/type-assertions

or

yarn add @maboroshi/type-assertions

使い方

Asserts API

TypeScript 3.7 より提供された Assertion Function を用いた型アサート機能を提供する。

import { Asserts } from '@maboroshi/type-assertions'

const fn = (value: number | null) => {
  Asserts.isNumber(value)
  return value.toString()
}

fn(123) // => `123`
fn(null) // => throw error!

Guards API

Type Guard 機能を提供する。

import { Guards } from '@maboroshi/type-assertions'

const fn = (value: number | null) => {
  if (Guards.isNumber(value)) {
    return value.toString()
  }
}

fn(123) // => `123`
fn(null) // => undefined

API

API ドキュメント を見る