3.0.1 • Published 9 months ago
@nlib/typing v3.0.1
@nlib/typing
A tool for generating and managing TypeScript type definitions efficiently.
npm install @nlib/typing
deno add jsr:@nlib/typing
import { typeChecker } from "@nlib/typing"
// Deno
import { typeChecker } from "jsr:@nlib/typing";
// Web
import { typeChecker } from 'https://esm.sh/@nlib/typing@3.0.0';
Usage
import * as assert from "node:assert";
import {
typeChecker,
ensure,
isString,
isPositiveSafeInteger,
isArrayOf,
isDictionaryOf,
isOptionalOf,
} from "@nlib/typing";
// For example, there is a interface named User.
interface User {
id: number;
name: string;
}
// You can create a TypeChecker for User with typeChecker().
const isUser = typeChecker({
id: isPositiveSafeInteger,
name: isString,
});
// typeChecker<T>() returns TypeChecker<T> which is a type guard for T.
// https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
// i.e. TypeChecker<User> is (value: unknown) => value is User
assert.equal(isUser({ id: 1, name: "a" }), true);
assert.equal(isUser({ id: "1", name: "a" }), false);
// You can handle a response with confidence using ensure().
const response = await fetch("https://jsonplaceholder.typicode.com/users/1");
const member = ensure(await response.json(), isUser);
console.info(`member.id: ${member.id}`);
console.info(`member.name: ${member.name}`);
// isArrayOf returns TypeChecker<Array<T>>.
const isUserArray = isArrayOf(isUser);
assert.equal(
isUserArray([
{ id: 1, name: "a" },
{ id: 2, name: "b" },
{ id: 3, name: "c" },
]),
true,
);
// isDictionaryOf returns TypeChecker<Record<string, T>>.
const isUserDictionary = isDictionaryOf(isUser);
assert.equal(
isUserDictionary({
a: { id: 1, name: "a" },
b: { id: 2, name: "b" },
c: { id: 3, name: "c" },
}),
true,
);
// isOptionalOf returns TypeChecker<T | undefined>.
const isItem = typeChecker({
id: isPositiveSafeInteger,
name: isOptionalOf(isString),
});
assert.equal(isItem({ id: 1 }), true);
assert.equal(isItem({ id: 1, name: "a" }), true);
assert.equal(isItem({ id: 1, name: 1 }), false);
// Example: Tree structure
interface Node {
id: string;
children?: Array<Node>;
}
const isNode = typeChecker({
// You can use regular expression for defining a string pattern.
id: /^[0-9a-z]+$/,
// You can't use isNode here because isNode is not defined yet.
// Use getter for recursive type definition.
get children() {
return isOptionalOf(isArrayOf(isNode));
},
});
assert.equal(isNode({ id: "a" }), true);
assert.equal(isNode({ id: "a", children: [] }), true);
assert.equal(isNode({ id: "a", children: [{ id: "b" }] }), true);
assert.equal(
isNode({ id: "a", children: [{ id: "b", children: [{ id: "c" }] }] }),
true,
);
assert.equal(
isNode({ id: "a", children: [{ id: "b", children: [{ id: "C" }] }] }),
// id: "C" is invalid because it is not lowercase.
false,
);
2.0.7
9 months ago
2.0.6
9 months ago
2.0.8
9 months ago
3.0.1
9 months ago
3.0.0
9 months ago
2.1.0
9 months ago
2.0.5
9 months ago
2.0.4
9 months ago
2.0.3
9 months ago
2.0.2
9 months ago
2.0.0
9 months ago
1.1.1
9 months ago
1.1.0
2 years ago
1.0.0
2 years ago
0.4.9
3 years ago
0.5.7
3 years ago
0.4.8
3 years ago
0.4.10
3 years ago
0.4.11
3 years ago
0.4.12
3 years ago
0.5.4
3 years ago
0.4.5
3 years ago
0.5.3
3 years ago
0.4.4
3 years ago
0.5.6
3 years ago
0.4.7
3 years ago
0.5.5
3 years ago
0.4.6
3 years ago
0.5.0
3 years ago
0.4.0
3 years ago
0.5.2
3 years ago
0.4.3
3 years ago
0.5.1
3 years ago
0.4.2
3 years ago
0.3.2
3 years ago
0.3.4
3 years ago
0.3.3
3 years ago
0.3.0
3 years ago
0.2.3
3 years ago
0.3.1
3 years ago
0.2.2
4 years ago
0.2.1
4 years ago
0.2.0
5 years ago
0.1.2
5 years ago
0.1.4
5 years ago
0.1.1
5 years ago
0.0.4
5 years ago
0.0.3
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago