Skip to content

Migration (v2 to v3)

Λlisue (Ali sue・ありすえ) edited this page Aug 2, 2024 · 3 revisions

Real world examples

Details

Use assert, ensure, or maybe function with corresponding is* predicate functions instead of assert*, ensure*, or maybe*

Before

import {
  assertString,
  ensureString,
  maybeString,
} from "https://deno.land/x/[email protected]/mod.ts";

const x: unknown = "Hello";
assertString(x);
const _ = ensureString(x);
const __ = maybeString(x) ?? "default";

After

import {
  assert,
  ensure,
  is,
  maybe,
} from "https://deno.land/x/[email protected]/mod.ts";

const x: unknown = "Hello";
assert(x, is.String);
const _ = ensure(x, is.String);
const __ = maybe(x, is.String) ?? "default";

Use is.ArrayOf function instead of the second argument of isArray

Before

import {
  isArray,
  isString,
} from "https://deno.land/x/[email protected]/mod.ts";

const x: unknown = ["a", "b", "c"];
if (isArray(x, isString)) {
  // ...
}

After

import { is } from "https://deno.land/x/[email protected]/mod.ts";

const x: unknown = ["a", "b", "c"];
if (is.ArrayOf(is.String)(x)) {
  // ...
}

Use is.Record function instead of isObject

Before

import { isObject } from "https://deno.land/x/[email protected]/mod.ts";

const x: unknown = { a: "a", b: "b", c: "c" };
if (isObject(x)) {
  // ...
}

After

import { is } from "https://deno.land/x/[email protected]/mod.ts";

const x: unknown = { a: "a", b: "b", c: "c" };
if (is.Record(x)) {
  // ...
}

Use is.RecordOf function instead of the second argument of isObject

Before

import {
  isObject,
  isString,
} from "https://deno.land/x/[email protected]/mod.ts";

const x: unknown = { a: "a", b: "b", c: "c" };
if (isObject(x, isString)) {
  // ...
}

After

import { is } from "https://deno.land/x/[email protected]/mod.ts";

const x: unknown = { a: "a", b: "b", c: "c" };
if (is.RecordOf(is.String)(x)) {
  // ...
}

Use is.*Of functions instead of isLike

Before

import { isLike } from "https://deno.land/x/[email protected]/mod.ts";

const a: unknown = ["a", 0, "b"];
if (isLike(["", 0, ""], a)) {
  // ...
}

const c: unknown = { foo: "foo", bar: 100 };
if (isLike({ foo: "", bar: 0 }, a)) {
  // ...
}

After

import { is } from "https://deno.land/x/[email protected]/mod.ts";

const a: unknown = ["a", 0, "b"];
// NOTE that `isTupleOf` require `readonly` array (`as const`)
if (is.TupleOf([is.String, is.Number, is.String] as const)(x)) {
  // ...
}

const c: unknown = { foo: "foo", bar: 100 };
if (is.ObjectOf({ foo: is.String, bar: is.Number })(a)) {
  // ...
}