1.2.9 • Public • Published


MakeStruct is a lightweight fast dynamic constructor generator for JavaScript.

The aim of makeStruct is to easily generate objects with dynamic properties on the fly.


A struct is used for building data structures. These structures or records are used to group related data together.


To install with npm:

npm i makestruct


makeStruct( 'keys' ) : Constructor

let myModel = 'example,foo, baz, the lazy fox jumps, over the, rainbow';
const MyDynamicClass = new makeStruct(myModel);
const myDynamicObject = new MyDynamicClass();

Object.keys(myDynamicObject); // ["example","foo","baz","theLazyFoxJumps","overThe","rainbow", ... methods]

Constructor definition:

 * @constructor Generates a constructor for a given data structure
 * @param {string} keys comma separated.
 * @returns {constructor} Constructor for the new struct


propagateArray( Array ) : void

Populates a key-value array into the Object instance properties.

const myData = [
  ['id', 1],
  ['name', 'John'],
  ['email', ''],
const MyDynamicClass = new makeStruct('id, name, email');
const myDynamicObject = new MyDynamicClass();

// myDynamicObject { id: 1, name: 'John', email: '' }

propagateObject( Object ) : void

Populates an Object into the Object instance properties.

const myData = {
  id: 1,
  name: 'John',
  email: '',
const MyDynamicClass = new makeStruct('id, name, email');
const myDynamicObject = new MyDynamicClass();

// myDynamicObject { id: 1, name: 'John', email: '' }

hasKey( 'key' ) : boolean

myDynamicObject.hasKey('name'); // true
myDynamicObject.hasKey('nome'); // false

hasValue( 'value' ) : boolean

myDynamicObject.hasValue('John'); // true
myDynamicObject.hasValue('Johnny'); // false

toArray() : Array

myDynamicObject.toArray(); // [['id', 1], ['name', 'John'], ['email', '']]


const Dog = new makeStruct('id, name, breed');
// Dog -> Function, constructor()
const myDog = new Dog(1, 'baxter', 'New Scotland Retriever');
myDog; // { id: 1, name: 'baxter', breed: 'New Scotland Retriever' }; // returns 1; // returns 'baxter'
myDog.breed; // returns 'New Scotland Retriever'

Type information about the example above

typeof; // 'number'
typeof myDog.breed; // 'string'
typeof myDog; // 'object'
myDog instanceof Dog; // true
Dog.prototype.isPrototypeOf(myDog); // true


// in a module (like in a React component)
import makeStruct from 'makestruct';
// outside module (like in Node JS)
const makestruct = require('makestruct');

Define some structure:

const User = new makeStruct('id, name, country');

Instantiate your new structure

const foo = new User(1, 'John', 'UK');

Access the struct properties; // 1; // 'john'; // 'UK'

Struct inside a struct

// Define a structure
const User = new makeStruct('id, name, country');
// Define another structure
const UserDetails = new makeStruct('phone, age, hairColor');
// Instantiate the inner struct first
const johnInfo = new UserDetails('555-777-888', 31, 'blonde');
// instantiate the parent struct passing the child struct as param
const john = new User(1, 'John', 'US', johnInfo);

// Accessing parent struct properties; // 1; // John; // 'US'

// Accessing child struct properties; // '555-777-888'; // 31; // 'blonde'

You can add structs into structs into other structs... just like a matroska.

The latest information I've relative to the maximum amount of keys in a JavaScript Object is around 8.3 million in V8 so that would be the limit approximately.


You can define your data structures in JavaScript with TS pragma and JSDoc like that:

// @ts-check

 * @typedef UserInfo
 * @property {string} phone
 * @property {number} age
 * @property {string} hairColor

/** @type {ObjectConstructor|any} */
const UserInfo = new makeStruct('phone, age, hairColor');

/** @type {UserInfo} */
const extraInfo = new UserInfo('555-777-888', 31, 'blonde');

TypeScript example:

type Dog = {
  id: number;
  name: string;
  breed: string;

const LeDog = new makeStruct('id, name, breed');
const myDog: Dog = new LeDog(1, 'baxter', 'Retriever');; // Property 'includes' does not exist on type 'number'.ts(2339)

Dynamically creating structures from a third party Object

const myAPIResponse = {
  id: 1,
  name: 'John',
  userInfo: {
    city: 'NY',
    phone: '555-666-777',
  pets: ['Baxter', 'Flurfils'],

const MyDynamicClass = new makeStruct(Object.keys(myAPIResponse).toString());
const myDynamicObject = new MyDynamicClass();

myDynamicObject.propagate(myAPIResponse);; // 'John'
myDynamicObject.pets; // ['Baxter', 'Flurfils']
myDynamicObject.userInfo; // { city: 'NY', phone: '555-666-777'}


  • Added support for dynamically create structures from a third party Object

Project Status

  • The project is provided as is without warranties of any kind.
  • I may add d.ts in future updates but ts-pragma along with JSDoc should suffice to export types for the constructor that returns a constructor. You'll need to define your own types for the objects you create with makeStruct either way.
  • If you face any problem feel free to open a new issue and I'll try to look at it asap.
  • Same applies for any pull request. The project is open source and It's maintained through collaborators or by my own.

Package Sidebar


npm i makestruct

Weekly Downloads






Unpacked Size

9.79 kB

Total Files


Last publish


  • beas7