https://github.com/codelytv/typescript-primitives-type
π§© TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)
https://github.com/codelytv/typescript-primitives-type
typescript typescript-library typescript-types typescript-utilities typescript-utility
Last synced: 8 months ago
JSON representation
π§© TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)
- Host: GitHub
- URL: https://github.com/codelytv/typescript-primitives-type
- Owner: CodelyTV
- License: gpl-3.0
- Created: 2022-05-26T13:32:47.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-12T14:35:59.000Z (10 months ago)
- Last Synced: 2025-04-04T03:38:56.534Z (9 months ago)
- Topics: typescript, typescript-library, typescript-types, typescript-utilities, typescript-utility
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@codelytv/primitives-type
- Size: 400 KB
- Stars: 119
- Watchers: 5
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
π§© TypeScript Primitives type
TypeScript utility type in order 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`:
```typescript
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:
```json
{
"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 modelled 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` utility type guarantees. Let's add it! πͺ
```typescript
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 {
return {
courseId: this.courseId.value,
courseTitle: this.courseTitle.value,
};
}
}
```
Done! βοΈ
The `Primitives` 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 Avanzado: MΓ‘s allΓ‘ de any](https://pro.codely.com/library/typescript-avanzado-mas-alla-de-any-182513/418230/about/) (Spanish - Course)
- [π DDD en TypeScript: Estructura de carpetas](https://youtu.be/AJJRk7qmVHg) (Spanish - YouTube video)
- [π Domain-Driven Design en TypeScript](https://pro.codely.com/library/ddd-en-typescript-modelado-y-arquitectura-172533/375662/about/) (Spanish - Course)
- [π₯ JavaScript moderno: Buenas prΓ‘cticas para empezar y refactorizar aplicaciones](https://pro.codely.com/library/javascript-moderno-buenas-practicas-para-empezar-y-refactorizar-aplicaciones-69571/208928/about/) (Spanish - Course)
- [ποΈ De JavaScript a TypeScript](https://pro.codely.com/library/de-javascript-a-typescript-128106/347481/about/) (Spanish - Course)
## π· TypeScript skeletons
- [π± TypeScript Basic Skeleton](https://github.com/CodelyTV/typescript-basic-skeleton): Bootstrap your new TypeScript frontend project
- [π TypeScript API Skeleton](https://github.com/CodelyTV/typescript-api-skeleton): Bootstrap your new TypeScript backend project
## π TypeScript Domain-Driven Design repositories
- [β¨ TypeScript DDD Skeleton](https://github.com/CodelyTV/typescript-ddd-skeleton): Bootstrap your new TypeScript projects applying Hexagonal Architecture and Domain-Driven Design patterns
- [π TypeScript DDD Course](https://github.com/CodelyTV/typescript-ddd-course): Learn Domain-Driven Design in TS lesson by lesson
- [π― TypeScript DDD Example](https://github.com/CodelyTV/typescript-ddd-example): Complete project applying Hexagonal Architecture and Domain-Driven Design patterns
## π― Other languages Domain-Driven Design repositories
- [βπ― Java DDD Example](https://github.com/CodelyTV/java-ddd-example)
- [ππ― PHP DDD Example](https://github.com/CodelyTV/php-ddd-example)
- [Ξ»π― Scala DDD Example](https://github.com/CodelyTV/scala-ddd-example)
- [π¦β¨ C# DDD Skeleton](https://github.com/CodelyTV/csharp-ddd-skeleton)