New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@albud/kind

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@albud/kind

TypeScript friendly library for creating classes with automatic validation and type conversion

latest
Source
npmnpm
Version
0.1.7
Version published
Maintainers
1
Created
Source

@albud/kind

Create classes with automatic validation and type conversion in TypeScript/JavaScript.

Note: kind is designed for constructing data-centric classes with properties and simple methods. It may not be good fit if that doesn't sound like the kinds of classes you want to construct.

Benefits

The key advantages of using kind over traditional class definitions:

  • Boilerplate reduction - No more repetitive constructor code
  • Automatic validation - Input data is validated and converted to correct types
  • Type safety - Full TypeScript support with proper type inference

Basic Usage

A simple example showing how to create a class with properties and methods:

import { kind } from "@albud/kind";

const Person = kind({
  name: String,
  age: Number,
  get greeting() {
    return `Hello, I'm ${this.name} and I'm ${this.age}`;
  }
  greet() {
    console.log(this.greeting)
  }
});

// Automatically validates and converts types
const john = new Person({ name: "John", age: 30 });
console.log(john.name); 
console.log(john.age);  
console.log(john.greeting)
john.greet()

Advanced Features

Explore more sophisticated patterns and capabilities:

Optional Properties

import { kind, optional } from "@albud/kind";

const User = kind({
  name: String,
  email: optional(String),
});

const user = new User({ name: "John" }); // email is undefined

Arrays

import { kind, array } from "@albud/kind";

const TodoList = kind({
  items: array(String),
  completed: array(Boolean),
});

const todos = new TodoList({
  items: ["Task 1", 2, true],        // ["Task 1", "2", "true"]
  completed: ["true", 0, 1]          // [true, false, true]
});

Custom Types

class Email extends String {
  constructor(email: string) {
    if (!email.includes("@")) {
      throw new Error("Invalid email format");
    }
    super(email.toLowerCase());
  }
}

const User = kind({
  name: String,
  email: Email,
  extraEmails: array(Email),  // Arrays of custom types work too
});

const user = new User({
  name: "John",
  email: "John@Example.COM",  // Converted to "john@example.com"
  extraEmails: ["admin@site.com", "user@site.com"]
});

Custom Base Class

class BaseEntity {
  id = Math.random();
  createdAt = new Date();
  
  save() {
    console.log(`Saving entity ${this.id}`);
  }
}

const User = kind({
  name: String,
  email: String,
}, BaseEntity);

const user = new User({ name: "John", email: "john@example.com" });
user.save(); // Method from BaseEntity
console.log(user.id); // Property from BaseEntity

Value Construction

How kind transforms input data using constructor functions:

  • String: Converts any value to string
  • Number: Converts strings/booleans to numbers (throws on invalid)
  • Boolean: Converts strings ("false"/"0"/"" → false, others → true)
  • Date: Converts strings/numbers to Date objects
  • Custom constructors: Uses new Constructor(value)

Installation

How to add kind to your project:

# npm
npm install @albud/kind

# yarn
yarn add @albud/kind

# pnpm
pnpm add @albud/kind
import { kind, optional, array } from "@albud/kind";

But Why?

See the difference between kind and traditional class construction:

Here's what the same Person class looks like without kind:

class Person {
  name: string;
  age: number;

  constructor(data: { name: string | number; age: string | number }) {
    if (typeof data.name !== 'string') {
      this.name = String(data.name);
    } else {
      this.name = data.name;
    }
    
    if (typeof data.age === 'string') {
      this.age = Number(data.age);
      if (isNaN(this.age)) {
        throw new Error('Invalid age');
      }
    } else {
      this.age = data.age;
    }
  }

  greet() {
    return `Hello, I'm ${this.name} and I'm ${this.age} years old`;
  }
}

That's 20+ lines of boilerplate for what kind does in 8 lines, and kind handles edge cases you might forget.

Keywords

typescript

FAQs

Package last updated on 15 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts