https://github.com/barelyhuman/typeable
a on-the-fly type generator for objects in node
https://github.com/barelyhuman/typeable
Last synced: 2 months ago
JSON representation
a on-the-fly type generator for objects in node
- Host: GitHub
- URL: https://github.com/barelyhuman/typeable
- Owner: barelyhuman
- License: mit
- Created: 2023-01-20T15:33:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-05T18:47:14.000Z (over 2 years ago)
- Last Synced: 2025-04-15T02:12:33.376Z (2 months ago)
- Language: JavaScript
- Size: 66.4 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typeable
> a on-the-fly type generator for objects in node
> **Warning**: This will not really work in a browser, it expects a filesystem
## Installation
Install `@barelyhuman/typeable` with npm
```bash
npm install @barelyhuman/typeable
```## What and Why
It's a generator for object interfaces. I have a project that I'm working on
which has a ton of shared objects being defined during development, this makes
it hard to keep track of what all was defined on the object.Typescript has cross file inference but still doesn't help with dynamically
defined properties so this helps generate a declarations file that stores all
the properties added to the object. There's definite knick knacks that can be
fixed.> **Note**: This is a subset requirement for one of my packages and **might
> not** be something you need for your day to day development. The generator's
> goal is to be able to generate types at runtime while in development.> **Note**: If the `NODE_ENV` is set to `production`, the type generator will
> not run.## Usage
```js
const { createTypeable } = require('@barelyhuman/typeable')/**
* @type {import("./typeable").App}
*/const app = createTypeable(
{}, // an empty object or reference to an object to be read
{
outfile: 'typeable.d.ts', // output path for the generated declaration file
rootInterfaceName: 'App', // the interface name to be generated for the above object.
}
)// add in properties, functions, methods, etc to the new typeable object.
app.property = {}
app.property.method = function () {}
``````ts
// typeable.d.ts
export interface App {
property: property
}interface property {
// generated generic functions right now
method: (...args: any[]) => any
}
```## Caveats
- Cannot handle `kebab-case` or `spaced keys` and will ignore them altogether
- Cannot infer the type of arguments from a function## License
[MIT](/LICENSE)