Coder Social home page Coder Social logo

assert-type's Introduction

@mr-hope/assert-type

作者: Mr.Hope License

一个简单的类型判断库。

assertType

interface TypeOption<T = unknown> {
    type: string | string[];
    enum?: T[];
    additional?: T[];
}

assertType<T = unknown>(variable: T, type: string[] | string | TypeOption, variableName?: string) => boolean;
案例
const a = 1;
const b = true;
const c = "abc";
const d = undefined;
const e = [];
const f = {};
const g = [1, 2, 3];
const h = ["a", "b", "c"];
const i = [1, "b", "c"];
const j = { a: 1, b: 2 };
const k = { a: "a", b: "b" };
const l = null;

assertType(a, "boolean"); // false
assertType(b, "boolean"); // true
assertType(b, "string"); // false
assertType(b, { type: "boolean", enum: [true] }); // true
assertType(c, "string"); // true
assertType(c, "undefined"); // false
assertType(d, "undefined"); // true
assertType(d, "number"); // false
assertType(a, "number"); // true
assertType(l, "null"); // true
assertType(l, "object"); // false
assertType(l, "number"); // false

assertType(e, "object"); // false
assertType(e, "array"); // true
assertType(f, "array"); // false
assertType(f, "object"); // true
assertType(g, "array"); // true
assertType(h, "array"); // true
assertType(i, "array"); // true
assertType(j, "object"); // true
assertType(k, "object"); // true

assertType(g, "string[]"); // false
assertType(g, "number[]"); // true
assertType(h, "number[]"); // false
assertType(h, "string[]"); // true
assertType(i, "number[]"); // false
assertType(i, "string[]"); // false
assertType(j, "Record<string,string>"); // false
assertType(j, "Record<string, string>"); // false
assertType(j, "Record<string,number>"); // true
assertType(j, "Record<string, number>"); // true
assertType(k, "Record<string,number>"); // false
assertType(k, "Record<string, number>"); // false
assertType(k, "Record<string,string>"); // true
assertType(k, "Record<string, string>"); // true

checkKeys

checkKeys<T = unknown>(obj: T, config: Record<string, string[] | string | TypeOption>, objName?: string) => boolean;
案例
checkKeys({ title: "a", desc: "b" }, { title: "string", desc: "string" }); // true

checkKeys(
  { title: "a", desc: "b" },
  { title: ["boolean", "string"], desc: ["string", "number"] },
); // true

checkKeys({ title: "a", desc: "b", a: 1 }, { title: "string", desc: "string" }); // false

checkKeys({ title: "a" }, { title: "string", desc: "string" }); // false

checkKeys({ title: "a", desc: 3 }, { title: "string", desc: "string" }); // false

checkKeys(
  { title: "a", desc: "b" },
  { title: "string", desc: "string", content: "array" },
); // false

assert-type's People

Contributors

dependabot[bot] avatar mister-hope avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

assert-type's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/test.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
npm
package.json
  • @types/node 20.14.8
  • @vitest/coverage-v8 1.6.0
  • conventional-changelog-cli 5.0.0
  • cz-conventional-changelog 3.3.0
  • esbuild 0.21.5
  • eslint 8.57.0
  • eslint-config-mister-hope 0.1.1
  • husky 9.0.11
  • nano-staged 0.8.0
  • prettier 3.3.2
  • rimraf 5.0.7
  • rollup 4.18.0
  • rollup-plugin-dts 6.1.1
  • rollup-plugin-esbuild 6.1.1
  • sort-package-json 2.10.0
  • tslib 2.6.3
  • typescript 5.5.2
  • vite 5.3.1
  • vitest 1.6.0
  • node >=18
  • pnpm 9.0.6

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.