From 56c61d03d1aa22a89511d6e62aac155ce02e35e4 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Mar 2026 11:41:56 +0000 Subject: [PATCH 1/2] Add TypeScript support with module-level type inference - Add index.d.ts with AbrequireModules interface for module augmentation - Overloaded use() signature: infers return type from AbrequireModules when path is registered, falls back to generic T or any - Update package.json with types field pointing to index.d.ts https://claude.ai/code/session_01N5DvsVCB3oVxB8qNNKGcvy --- index.d.ts | 26 ++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 27 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..ad85bb2 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,26 @@ +/** + * Register your module paths and types here via module augmentation: + * + * declare module 'abrequire' { + * interface AbrequireModules { + * 'app/db/mysql': typeof import('./app/db/mysql'); + * 'app/utils/helper': typeof import('./app/utils/helper'); + * } + * } + */ +export interface AbrequireModules {} + +/** + * Require a module using an absolute path from the project root. + * + * When the path is registered in AbrequireModules, the return type is inferred automatically. + * Otherwise, you can provide the type explicitly: use('app/my-module') + * + * @example + * const mysql = use('app/db/mysql') // typed if registered in AbrequireModules + * const helper = use('app/utils/helper') // explicit type + */ +declare function use(path: K): AbrequireModules[K]; +declare function use(path: string): T; + +export = use; diff --git a/package.json b/package.json index 9e05190..e8dfbee 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.0.4", "description": "abrequire is absolute path require", "main": "index.js", + "types": "index.d.ts", "repository": { "type": "git", "url": "https://github.com/7elven/abrequire" From 7eac33107d27330fcfc6100e171b04732375307d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Mar 2026 11:43:45 +0000 Subject: [PATCH 2/2] Update README with TypeScript usage documentation https://claude.ai/code/session_01N5DvsVCB3oVxB8qNNKGcvy --- README.md | 86 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 42c0a61..618a773 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,9 @@ When the directory structure of your Node.js application (not library!) has some const mysql = require('../../../../db/mysql') ``` Those suck for maintenance and they're ugly. [link](https://gist.github.com/branneman/8048520) + ## Install -```javascript +```bash npm install abrequire --save ``` @@ -22,29 +23,81 @@ const use = require('abrequire') ``` ## Example + +Project structure: +``` +root/ +├── app/ +│ ├── db/ +│ │ ├── mysql.js +│ │ └── mongoose.js +│ └── routes/ +│ └── group/ +│ └── subGroup/ +│ └── router.js +├── server.js +├── node_modules/ +└── package.json ``` --root ---app -----db -------mysql.js -------mongoose.js -----routes -------group ----------subGroup -------------router.js ---server.js ---node_module ---package.json -``` -When `router.js` require `mysql.js` relative path + +Instead of relative paths: ```javascript const mysql = require('../../../../db/mysql') ``` -When `router.js` require `mysql.js` absolute path + +Use absolute paths from the project root: ```javascript const use = require('abrequire') const mysql = use('app/db/mysql') ``` + +## TypeScript Support + +abrequire includes TypeScript type definitions out of the box. + +### Basic usage with explicit type + +Provide the type manually using a generic parameter: + +```typescript +import use = require('abrequire') + +const mysql = use('app/db/mysql') +``` + +### Automatic type inference via module augmentation + +For the best experience, register your module paths and their types once using +TypeScript's module augmentation. After that, `use()` will automatically infer +the correct return type for each registered path — no generics needed at the call site. + +**Step 1** — Create a declaration file in your project (e.g. `global.d.ts`): + +```typescript +declare module 'abrequire' { + interface AbrequireModules { + 'app/db/mysql': typeof import('./app/db/mysql') + 'app/db/mongoose': typeof import('./app/db/mongoose') + 'app/utils/helper': typeof import('./app/utils/helper') + } +} +``` + +**Step 2** — Use `use()` anywhere in your project with full type safety: + +```typescript +import use = require('abrequire') + +const mysql = use('app/db/mysql') // type is inferred automatically +const mongoose = use('app/db/mongoose') // type is inferred automatically +``` + +Unregistered paths fall back to `any`, so you can still use them freely: + +```typescript +const something = use('app/other/module') // type: any +``` + ## Authors * **Nonthachai Korninai** - [Github](https://github.com/7elven) @@ -52,4 +105,3 @@ const mysql = use('app/db/mysql') ## License This project is licensed under the MIT License -