@codelytv/primitives-type

中文文档
3.1.0发布于 2025/04/25GPL-3.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 1765

Type entity primitives from value objects

原始使用文档
文档目录4 节
  1. ☝️ Learning resources
  2. 🔷 TypeScript skeletons
  3. 🌈 TypeScript Domain-Driven Design repositories
  4. 🎯 Other languages Domain-Driven Design repositories

Codely logo

🧩 TypeScript Primitives type

Codely Open Source CodelyTV Courses

TypeScript utility type to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects).

Take a look, play and have fun with this. Stars are welcome 😊

👀 Usage example

You can have a domain entity such as the following Course:

import { CourseId } from "./CourseId";
import { CourseTitle } from "./CourseTitle";

export class Course {
  constructor(public courseId: CourseId, public courseTitle: CourseTitle) {}

  updateTitle(newCourseTitle: CourseTitle): void {
    this.courseTitle = newCourseTitle;
  }

  someOtherMethodWithDomainLogic(): void {
    // some algorithm
  }
}

When we want to have a toPrimitives method in order to pass an instance of the Course class between architectural layers, so the thing we want pass around should be to serialized/marshalled in a way that only containings primitive values such as:

{
  "courseId": "25658f31-2da1-4942-8b58-88aeacc79860",
  "courseTitle": "🚜 TypeScript Avanzado: Más allá de any"
}

That is:

  • We want to avoid including methods such as the thisFunctionShouldNotBeIncludedInTheToPrimitives one, so the transformation should only include properties.
  • We want to flatten or unwrap encapsulated properties such as the courseId and courseTitle ones. They are modeled as Value Objects (CourseId and CourseTitle classes), so the transformation or flatten should only include properties in a recursive manner.

That is exactly what our Primitives<T> utility type guarantees. Let's add it! 💪

import { Primitives } from "@codelytv/primitives-type";

import { CourseId } from "./CourseId";
import { CourseTitle } from "./CourseTitle";

export class Course {
  constructor(public courseId: CourseId, public courseTitle: CourseTitle) {}

  updateTitle(newCourseTitle: CourseTitle): void {
    this.courseTitle = newCourseTitle;
  }

  someOtherMethodWithDomainLogic(): void {
    // some algorithm
  }

  toPrimitives(): Primitives<Course> {
    return {
      courseId: this.courseId.value,
      courseTitle: this.courseTitle.value,
    };
  }
}

Done! ✌️

The Primitives<Course> return type is ensuring all our restrictions in a very straightforward way! 🌈

👍 How to install

  • Npm: npm install --save-dev @codelytv/primitives-type
  • Yarn: yarn add -D @codelytv/primitives-type

👌 Codely Code Quality Standards

Publishing this package we are committing ourselves to the following code quality standards:

  • 🤝 Respect Semantic Versioning: No breaking changes in patch or minor versions
  • 🤏 No surprises in transitive dependencies: Use the bare minimum dependencies needed to meet the purpose
  • 🎯 One specific purpose to meet without having to carry a bunch of unnecessary other utilities
  • ✅ Tests as documentation and usage examples
  • 📖 Well documented ReadMe showing how to install and use
  • ⚖️ License favoring Open Source and collaboration

🔀 Related utilities and resources

☝️ Learning resources

🔷 TypeScript skeletons

🌈 TypeScript Domain-Driven Design repositories

🎯 Other languages Domain-Driven Design repositories

版本与发布记录

@codelytv/primitives-type 共发布 13 个版本,首次发布于 2022/05/26;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.1.0latest2025/04/251.5 年前
3.0.22025/04/251.5 年前
2.0.02025/02/041.8 年前
1.2.02025/02/041.8 年前
1.1.12024/09/062 年前
1.1.02024/09/042 年前

关于 @codelytv/primitives-type 的常见问题

@codelytv/primitives-type 是什么?

Type entity primitives from value objects

如何安装 @codelytv/primitives-type?

在项目目录运行 npm install @codelytv/primitives-type,或使用 pnpm add / yarn add / bun add。

@codelytv/primitives-type 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codelytv/primitives-type 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@codelytv/primitives-type 可以商用吗?

@codelytv/primitives-type 使用 GPL-3.0 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@codelytv/primitives-type 的下载量有多少?

根据 npm 官方下载统计,@codelytv/primitives-type 在 2026/10/10 – 2026/11/08 这 30 天内被下载 1,765 次,日均约 59 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codelytv/primitives-type 还在维护吗?

最新版本 3.1.0 发布于 2025/04/25(1.5 年前);至今共发布 13 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/05/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。