- v3.18.1Latest
- v3.18.0
- v3.17.3
- v3.17.2
- v3.17.1
- v3.17.0
- v3.16.3
- v3.16.2
- v3.16.1
- v3.16.0
- v3.15.0
- v3.14.1
- v3.14.0
- v3.13.0
- v3.12.1
- v3.12.0
- v3.11.0
- v3.10.0
- v3.9.0
- v3.8.0
- v3.7.0
- v3.6.0
- v3.5.1
- v3.5.1
- v3.5.0
- v3.4.0
- v3.3.1
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.1
- v3.0.0
- v2.1.1
- v2.1.0
- v2.0.0
- v1.2.1
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v0.1.1
- v0.1.0
unknownutil
A utility pack for handling unknown
type.
Usage
It provides is
module for type predicate functions and assert
, ensure
, and
maybe
helper functions.
is*
Type predicate function is a function which returns true
if a given value is
expected type. For example, isString
(or is.String
) returns true
if a
given value is string
.
import { is } from "https://deno.land/x/unknownutil@v3.15.0/mod.ts";
const a: unknown = "Hello";
if (is.String(a)) {
// 'a' is 'string' in this block
}
Additionally, is*Of
(or is.*Of
) functions return type predicate functions to
predicate types of x
more precisely like:
import {
is,
PredicateType,
} from "https://deno.land/x/unknownutil@v3.15.0/mod.ts";
const isArticle = is.ObjectOf({
title: is.String,
body: is.String,
refs: is.ArrayOf(
is.OneOf([
is.String,
is.ObjectOf({
name: is.String,
url: is.String,
}),
]),
),
});
type Article = PredicateType<typeof isArticle>;
const a: unknown = {
title: "Awesome article",
body: "This is an awesome article",
refs: [{ name: "Deno", url: "https://deno.land/" }, "https://github.com"],
};
if (isArticle(a)) {
// a is narrowed to the type of `isArticle`
console.log(a.title);
console.log(a.body);
for (const ref of a.refs) {
if (is.String(ref)) {
console.log(ref);
} else {
console.log(ref.name);
console.log(ref.url);
}
}
}
assert
The assert
function does nothing if a given value is expected type. Otherwise,
it throws an AssertError
exception like:
import {
assert,
is,
} from "https://deno.land/x/unknownutil@v3.15.0/mod.ts";
const a: unknown = "Hello";
// `assert` does nothing or throws an `AssertError`
assert(a, is.String);
// a is now narrowed to string
// With custom message
assert(a, is.String, { message: "a must be a string" });
ensure
The ensure
function return the value as-is if a given value is expected type.
Otherwise, it throws an AssertError
exception like:
import {
ensure,
is,
} from "https://deno.land/x/unknownutil@v3.15.0/mod.ts";
const a: unknown = "Hello";
// `ensure` returns `string` or throws an `AssertError`
const _: string = ensure(a, is.String);
// With custom message
const __: string = ensure(a, is.String, { message: "a must be a string" });
maybe
The maybe
function return the value as-is if a given value is expected type.
Otherwise, it returns undefined
that suites with
nullish coalescing operator (??
)
like:
import {
is,
maybe,
} from "https://deno.land/x/unknownutil@v3.15.0/mod.ts";
const a: unknown = "Hello";
// `maybe` returns `string | undefined` so it suites with `??`
const _: string = maybe(a, is.String) ?? "default value";
Migration
See GitHub Wiki for migration to v3 from v2 or v2 from v1.
License
The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.