MCPcopy Index your code
hub / github.com/CodelyTV/typescript-primitives-type

github.com/CodelyTV/typescript-primitives-type @v3.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.1.0 ↗ · + Follow
50 symbols 52 edges 20 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Codely logo

🧩 TypeScript Primitives type

<a href="https://github.com/CodelyTV"><img src="https://img.shields.io/badge/CodelyTV-OS-green.svg?style=flat-square" alt="Codely Open Source"/></a>
<a href="https://pro.codely.com"><img src="https://img.shields.io/badge/CodelyTV-PRO-black.svg?style=flat-square" alt="CodelyTV Courses"/></a>

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

Core symbols most depended-on inside this repo

constructor
called by 0
tests/User.ts
toPrimitives
called by 0
tests/User.ts
constructor
called by 0
tests/Metadata.ts
toPrimitives
called by 0
tests/Metadata.ts
constructor
called by 0
tests/Product.ts
toPrimitives
called by 0
tests/Product.ts
constructor
called by 0
tests/DeliveryInfo.ts
toPrimitives
called by 0
tests/DeliveryInfo.ts

Shape

Class 29
Method 19
Function 2

Languages

TypeScript100%

Modules by API surface

tests/Course.ts5 symbols
tests/Video.ts4 symbols
tests/User.ts4 symbols
tests/Step.ts4 symbols
tests/Product.ts4 symbols
tests/Metadata.ts4 symbols
tests/Learner.ts4 symbols
tests/DeliveryInfo.ts4 symbols
tests/Address.ts4 symbols
tests/StringValueObject.ts3 symbols
tests/VideoName.ts2 symbols
tests/VideoId.ts2 symbols

For agents

$ claude mcp add typescript-primitives-type \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact