{"_id":"@dongxiang678/typechecker","name":"@dongxiang678/typechecker","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@dongxiang678/typechecker","version":"1.0.0","description":"A lightweight and flexible type checker for JavaScript, enforce strong template-type checking on functions and classes","main":"src/checker.js","scripts":{"test":"node test/checker.test.js","prepublishOnly":"npm test"},"keywords":["typeChecker","type-checking","template-type","type-validation","javascript","type-safety","type check","template","strong type","javascript","type safety","function type","class type"],"author":{"name":"董翔","email":"3631247406@qq.com","url":"https://www.npmjs.com/~dongxiang678"},"license":"MIT","copyright":"Copyright (c) 2026 董翔 (dongxiang678)","repository":{"type":"git","url":"git+https://github.com/dongxiang678/typeChecker.git"},"bugs":{"url":"https://github.com/dongxiang678/typeChecker/issues"},"homepage":"https://github.com/dongxiang678/typeChecker#readme","engines":{"node":">=12.0.0"},"type":"commonjs","_id":"@dongxiang678/typechecker@1.0.0","_nodeVersion":"22.15.0","_npmVersion":"11.4.2","dist":{"integrity":"sha512-wa2kSsb211fJXm37L7cW/QbMfuI/o/qxTZZIW+G15nfilq3oX/ht2m9at4v0L6saHrR5QSRVfidtv0qy/tvQhw==","shasum":"1cd1aa6557841983cd1fba9aae29f50f1310b769","tarball":"https://registry.npmjs.org/@dongxiang678/typechecker/-/typechecker-1.0.0.tgz","fileCount":4,"unpackedSize":13942,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIBaj3ZLCNQl9tUfpLjlG9OywZDV9q/nvj4vGb6rNWxzKAiA2Kwd3K7z7f3/wkPgeODfR0ie8SYhbMuXpODL63YkA2g=="}]},"_npmUser":{"name":"dongxiang678","email":"3631247406@qq.com"},"directories":{},"maintainers":[{"name":"dongxiang678","email":"3631247406@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/typechecker_1.0.0_1773367350347_0.022686578726633266"},"_hasShrinkwrap":false}},"time":{"created":"2026-03-13T02:02:30.275Z","1.0.0":"2026-03-13T02:02:30.499Z","modified":"2026-03-13T02:02:30.679Z"},"maintainers":[{"name":"dongxiang678","email":"3631247406@qq.com"}],"description":"A lightweight and flexible type checker for JavaScript, enforce strong template-type checking on functions and classes","homepage":"https://github.com/dongxiang678/typeChecker#readme","keywords":["typeChecker","type-checking","template-type","type-validation","javascript","type-safety","type check","template","strong type","javascript","type safety","function type","class type"],"repository":{"type":"git","url":"git+https://github.com/dongxiang678/typeChecker.git"},"author":{"name":"董翔","email":"3631247406@qq.com","url":"https://www.npmjs.com/~dongxiang678"},"bugs":{"url":"https://github.com/dongxiang678/typeChecker/issues"},"license":"MIT","readme":"\r\n# typeChecker\r\n\r\nA lightweight, flexible and template-based type checker for JavaScript. Enforce strong type safety on functions and classes without TypeScript.\r\n\r\n> 📦 NPM 包名：`@dongxiang678/typechecker`  \r\n> 安装：`npm install @dongxiang678/typechecker --save`\r\n\r\n## Features\r\n- ✅ Template-based type constraint (support all JS types: Number/String/Array/Custom Class)\r\n- ✅ Strong type checking for function parameters & return values\r\n- ✅ No implicit type conversion between different types\r\n- ✅ Lightweight (0 dependencies), easy to integrate\r\n- ✅ Compliant with JavaScript ecosystem norms (suitable for large projects)\r\n\r\n## Installation\r\n```bash\r\nnpm install @dongxiang678/typechecker --save\r\n# or yarn\r\nyarn add @dongxiang678/typechecker\r\n```\r\n\r\n## Quick Start\r\n### 1. Basic Type Check\r\n```javascript\r\nconst { createCheckerTemplate, enforceReturnType } = require('@dongxiang678/typechecker');\r\n\r\n// Create a checker template for String type\r\nconst StringChecker = createCheckerTemplate(String);\r\n\r\n// Enforce function to return StringChecker\r\nconst getString = enforceReturnType(StringChecker, (value) => {\r\n  const checker = new StringChecker();\r\n  return checker.check(value);\r\n});\r\n\r\n// Success\r\nconsole.log(getString(\"Hello typeChecker!\").getValue()); // \"Hello typeChecker!\"\r\n\r\n// Error: Number type cannot be converted to String\r\ntry {\r\n  getString(123);\r\n} catch (err) {\r\n  console.error(err.message); // \"Return value type error: Checker<Number> cannot be converted to Checker<String>\"\r\n}\r\n```\r\n\r\n### 2. Custom Class Check\r\n```javascript\r\nconst { createCheckerTemplate, enforceFunctionType } = require('typeChecker');\r\n\r\nclass User {\r\n  constructor(name) {\r\n    this.name = name;\r\n  }\r\n}\r\n\r\n// Create a checker template for User class\r\nconst UserChecker = createCheckerTemplate(User);\r\n\r\n// Enforce parameter & return type (UserChecker)\r\nconst getUser = enforceFunctionType([UserChecker], UserChecker, (checker) => {\r\n  return checker.check(new User(\"张三\"));\r\n});\r\n\r\n// Success\r\nconst userChecker = new UserChecker(new User(\"李四\"));\r\nconsole.log(getUser(userChecker).getValue().name); // \"张三\"\r\n```\r\n\r\n## API\r\n### `createCheckerTemplate(targetType)`\r\nCreate a type checker template for specific type.\r\n- `targetType`: Function (e.g., `String`, `User`) or string (e.g., \"Number\")\r\n- Returns: Checker class with type constraint\r\n\r\n### `enforceFunctionType(paramCheckers, returnChecker, fn)`\r\nEnforce type checking for function parameters and return value.\r\n\r\n### `enforceReturnType(returnChecker, fn)`\r\nSimplified: only enforce return value type.\r\n\r\n## Why Choose This?\r\n- Solve type-related bugs in pure JavaScript projects\r\n- No TypeScript required, but achieve strong type safety\r\n- Lightweight and modular (works with any project scale)\r\n- Intuitive API, easy to learn and use\r\n\r\n## License\r\nMIT\r\n```\r\n","readmeFilename":"README.md","_rev":"1-7ec930469063f53745edbae21919f8c6"}