diff --git a/changelog.md b/changelog.md index 39f084a..a31cf26 100644 --- a/changelog.md +++ b/changelog.md @@ -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 --- diff --git a/src/index.ts b/src/index.ts index d096c71..dd386cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export type { Systemic, Component } from "./types"; +export type { Systemic, Component, RegistrationsOf, TypeOf } from "./types"; export { systemic } from "./systemic"; diff --git a/src/types/index.ts b/src/types/index.ts index ee549ff..c3d3605 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -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"; diff --git a/src/types/system.ts b/src/types/system.ts index d4bdf9a..ce46d81 100644 --- a/src/types/system.ts +++ b/src/types/system.ts @@ -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 @@ -24,3 +25,17 @@ type BuildSystem< export type ComponentsOf> = { [K in keyof TSystem]: TSystem[K]["component"]; }; + +/** + * Extract the registration type from a Systemic type + */ +export type RegistrationsOf> = TSystem extends Systemic + ? T + : never; + +/** + * Extract the component types from a Systemic type + */ +export type TypeOf> = TSystem extends Systemic + ? SystemOf + : never; diff --git a/test/types/system.spec.ts b/test/types/system.spec.ts index a526efe..df9ed97 100644 --- a/test/types/system.spec.ts +++ b/test/types/system.spec.ts @@ -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; - "baz.qux": Registration<{ qux: boolean }>; - "baz.quux": Registration; - }; - - type System = SystemOf; - - 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; + "baz.qux": Registration<{ qux: boolean }>; + "baz.quux": Registration; + }; + + type System = SystemOf; + + 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; + "baz.qux": Registration<{ qux: boolean }>; + "baz.quux": Registration; + }; + + type System = Systemic; + + expectTypes, Definition>().toBeEqual(); + }); + }); + + describe("TypeOf", () => { + it("extracts the component types from a Systemic type", () => { + type Definition = { + foo: Registration<{ foo: string }>; + bar: Registration; + "baz.qux": Registration<{ qux: boolean }>; + "baz.quux": Registration; + }; + + type System = Systemic; + + expectTypes< + TypeOf, + { foo: { foo: string }; bar: number; baz: { qux: { qux: boolean }; quux: string } } + >().toBeEqual(); + }); }); });