npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-data-objects

v1.1.0

Published

Simple lightweight library for enhancing plain JS objects with TypeScript wrappers enabling easy API data assertion and validation

Downloads

8

Readme

Simple, lightweight, and flexible TypeScript library for creating type-safe data objects with validation.

Implementation documentation can be found on https://salmon42.github.io/ts-data-objects.

Changelog can be found here


Table of Contents


Introduction

ts-data-objects provides a lightweight way to create, validate, and parse TypeScript objects with user-defined runtime type checking. Unlike more complex validation libraries, it focuses on the essential task of ensuring data matches your TypeScript types - particularly useful when working with API responses or JSON data.

The library provides a compromise in balance between type safety and simplicity, allowing you to use as much or as little of its functionality as you need - from simple object construction with defaults to complete type-safe parsing and validation.

Installation

You can install this package from NPM registry https://www.npmjs.com/package/ts-data-objects.

npm install ts-data-objects

Usage

The library is exported with multiple entry points to allow granular usage of the available functionality.

// Importing everything from single entry point
import { ... } from 'ts-data-objects'

// Importing only partial functionality
import { ... } from 'ts-data-objects/common'
import { ... } from 'ts-data-objects/core'
import { ... } from 'ts-data-objects/deep' // Currently only experimental

Basic Object Definition

import { defineObject, isStr, isNum } from 'ts-data-objects'

// Define your TypeScript type. It may clash with generated constructor
type User = {
  name: string
  age: number
  verified?: boolean
}

// Create a complete object definition with validation
const { User, isUser, parseUser } = defineObject<User>('User', {
  // Default values
  defaultValues: {
    verified: false
  },
  // Type validation predicate
  predicate: o => (
    isStr(o?.name) &&
    isNum(o?.age)
  ),
  // doNotThrow: true // -> if this is set to true, you don't need to catch any error, it will only log console.error by itself
})

// Create a new user with defaults. Must provide both name and age accorting to TS type/interface
const user1 = User({ name: 'John', age: 20 })
// Result: { name: 'John', age: 20, verified: false }

// Validate unknown data
if (isUser(someData)) {
  // TS now knows that someData is User
  console.log(someData.name, someData.age)
}

// Safely parse unknown data (throws on invalid data)
// Throwing behavior may be changed with doNotThrow param in defineObject
try {
  const user2 = parseUser(apiResponse)
  console.log(user2.name)
}
catch (e: any) {
  console.error(e)
}

Using Individual Components

The library can be used modularly - you don't need to use all features:

import { dataObject, dataGuard } from 'ts-data-objects/core'
import { isStr, isNum } from 'ts-data-objects/common'

type User = {
  name: string
  age: number
  verified?: boolean
}

// Just create objects with defaults
const User = dataObject<User>({
  verified: false,
  // Here you an provide any default-like values to fill
  // if you don't get them from API response or want them
  // to be created automatically.
})

// Only guard function
const isUser = dataGuard<User>(o => (
  isStr(o?.name) &&
  isNum(o?.age)
  // You can use either helping typechecking functions
  // from 'ts-data-objects/common'
  // or use your own validation logic.
))

Why ts-data-objects?

This library is not at all something new in the web development environment. We already have well known and battle-tested libraries like Zod and Yup. While these libraries are excellent for comprehensive schema validation, ts-data-objects takes a different approach:

✅ 1. Simplicity first

  • ts-data-objects focuses purely on TypeScript type validation
  • No complex schema definition language to learn
  • Uses standard TypeScript types and JavaScript type checks
  • Smaller learning curve

💪 2. Flexible

  • Availability of modular design - use only what you need
  • No lock-in to a specific validation paradigm
  • Works well as just a typed constructor utility

🔄 3. Type-System Aligned

  • Works with your existing TypeScript interfaces
  • No need to maintain or create separate schema definitions

☁ 4. Lightweight

  • Easy to integrate into existing project
  • No dependencies, small bundle size

When to choose ts-data-objects

ts-data-objects is ideal for projects that need straightforward type validation without the complexity of full schema validation libraries. It's particularly well-suited for API integration layers and internal data management where TypeScript types are the source of truth.

Choose ts-data-objects when you:

  • Need simple type validation for API responses
  • Want to ensure data matches TypeScript types at runtime
  • Need object construction with defaults
  • Prefer using standard TypeScript types instead of reconstructing them with 3rd party library schemas
  • Want a minimal, lightweight solution

Consider Zod or Yup when you need:

  • Complex validation rules (email formats, regex, etc.) out of the box
  • Value transformations and coercion
  • Advanced string/number constraints

License

This library is released under MIT license, which means that you can reuse any part of code here for your convenience.

Copyright (C) 2025-present, Andrej Hučko

  • ~