Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### What's changed

- Remove the need for marking a mapping dependency `as const`
- Export type helpers to get the registrations and type of the systemic

---

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type { Systemic, Component } from "./types";
export type { Systemic, Component, RegistrationsOf, TypeOf } from "./types";
export { systemic } from "./systemic";
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type { Component, ComponentTypeOf, DependenciesOf, IsComponent } from "./component";
export type { AsRegistrations, Definition, Registration } from "./definition";
export type { DependsOnOption } from "./dependencies";
export type { ComponentsOf, SystemOf } from "./system";
export type { ComponentsOf, SystemOf, RegistrationsOf, TypeOf } from "./system";
export type { Systemic, SystemicBuild } from "./systemic";
export type { EmptyObject } from "./util";
15 changes: 15 additions & 0 deletions src/types/system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Registration } from "./definition";
import type { Systemic } from "./systemic";
import type { EmptyObject, SetNestedProp, UnionToTuple } from "./util";

// Build the system type from the system definition
Expand All @@ -24,3 +25,17 @@ type BuildSystem<
export type ComponentsOf<TSystem extends Record<string, Registration>> = {
[K in keyof TSystem]: TSystem[K]["component"];
};

/**
* Extract the registration type from a Systemic type
*/
export type RegistrationsOf<TSystem extends Systemic<any>> = TSystem extends Systemic<infer T>
? T
: never;

/**
* Extract the component types from a Systemic type
*/
export type TypeOf<TSystem extends Systemic<any>> = TSystem extends Systemic<infer T>
? SystemOf<T>
: never;
65 changes: 50 additions & 15 deletions test/types/system.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
import type { Registration, SystemOf } from "../../src/types";
import type { Registration, RegistrationsOf, Systemic, SystemOf, TypeOf } from "../../src/types";
import { expectTypes } from "../test-helpers/type-matchers";

describe("system types", () => {
it("is the type of the system defined by the given definition", () => {
type Definition = {
foo: Registration<{ foo: string }>;
bar: Registration<number>;
"baz.qux": Registration<{ qux: boolean }>;
"baz.quux": Registration<string>;
};

type System = SystemOf<Definition>;

expectTypes<
System,
{ foo: { foo: string }; bar: number; baz: { qux: { qux: boolean }; quux: string } }
>().toBeEqual();
describe("SystemOf", () => {
it("is the type of the system defined by the given definition", () => {
type Definition = {
foo: Registration<{ foo: string }>;
bar: Registration<number>;
"baz.qux": Registration<{ qux: boolean }>;
"baz.quux": Registration<string>;
};

type System = SystemOf<Definition>;

expectTypes<
System,
{ foo: { foo: string }; bar: number; baz: { qux: { qux: boolean }; quux: string } }
>().toBeEqual();
});
});

describe("RegistrationsOf", () => {
it("extracts the registration type from a Systemic type", () => {
type Definition = {
foo: Registration<{ foo: string }>;
bar: Registration<number>;
"baz.qux": Registration<{ qux: boolean }>;
"baz.quux": Registration<string>;
};

type System = Systemic<Definition>;

expectTypes<RegistrationsOf<System>, Definition>().toBeEqual();
});
});

describe("TypeOf", () => {
it("extracts the component types from a Systemic type", () => {
type Definition = {
foo: Registration<{ foo: string }>;
bar: Registration<number>;
"baz.qux": Registration<{ qux: boolean }>;
"baz.quux": Registration<string>;
};

type System = Systemic<Definition>;

expectTypes<
TypeOf<System>,
{ foo: { foo: string }; bar: number; baz: { qux: { qux: boolean }; quux: string } }
>().toBeEqual();
});
});
});
Loading