Skip to content

Commit

Permalink
feat: make types more readable (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Sep 6, 2022
1 parent 40cb247 commit 408eb22
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"dependencies": {
"lodash.ismatch": "^4.4.0",
"remeda": "^1.0.0",
"ts-toolbelt": "^9.6.0",
"zod": "^3.17.3"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions src/data/types/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ObjectValues, OnlyStrings } from '../../lib/utils.js'
import { RecordController } from '../../record/types/controller.js'
import { StoredRecord } from '../../record/types/StoredRecord.js'
import { StoredADT } from './Builder.js'
import { Any } from 'ts-toolbelt'

export type SomeShortHandRecordSchemaDefs = Record<string, SomeSchemaDef>

Expand Down Expand Up @@ -67,9 +68,12 @@ export namespace DataController {

// prettier-ignore
export type DataController<ADT extends StoredADT, Vs extends StoredRecords> =
ADT &
ADTMethods<Vs> &
RecordsMethods<Vs>
Any.Compute<
ADT &
RecordsMethods<Vs> &
ADTMethods<Vs>,
'flat'
>

/**
* Build up the API on the ADT itself:
Expand All @@ -81,9 +85,9 @@ export type DataController<ADT extends StoredADT, Vs extends StoredRecords> =
*/
// prettier-ignore
type ADTMethods<Vs extends StoredRecords> = {
from: Any.Compute<DecoderMethods<'json', Vs> & StoredRecords.GetAdtLevelDecoderMethods<Vs>>
to: Any.Compute<EncoderMethods<'json', Vs> & StoredRecords.GetAdtLevelEncoderMethods<Vs>>
schema: StoredRecords.ZodUnion<Vs>
from: DecoderMethods<'json', Vs> & StoredRecords.GetAdtLevelDecoderMethods<Vs>
to: EncoderMethods<'json', Vs> & StoredRecords.GetAdtLevelEncoderMethods<Vs>
}

/**
Expand All @@ -95,8 +99,7 @@ type ADTMethods<Vs extends StoredRecords> = {
* ```
*/
export type RecordsMethods<Vs extends StoredRecords> = {
[V in Vs[number] as V[`name`]]: RecordController<Vs, V>
// [V in Vs[number] as V[`name`]]: V['schema']
[V in Vs[number] as V[`name`]]: Any.Compute<RecordController<Vs, V>, 'flat'>
}

// Helpers
Expand Down
36 changes: 20 additions & 16 deletions src/record/types/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { OmitRequired, Rest } from '../../lib/utils.js'
import { z } from '../../lib/z/index.js'
import { SomeDecodeOrThrowJson, SomeDecoderJson, SomeDefaultsProvider, SomeEncoderJson } from './internal.js'
import { SomeStoredRecord, StoredRecord } from './StoredRecord.js'
import { Any } from 'ts-toolbelt'

export type SomeRecord = {
_tag: string
Expand Down Expand Up @@ -67,16 +68,17 @@ type DecoderMethods<Name extends string, R extends SomeStoredRecord> = {
}

export namespace RecordController {
export type CreateFromSchema<Name extends SomeName, Schema extends SomeSchema> = CreateFromStoredRecord<
StoredRecord.AddSchema<Schema, StoredRecord.Create<Name>>
export type CreateFromSchema<Name extends SomeName, Schema extends SomeSchema> = Any.Compute<
CreateFromStoredRecord<StoredRecord.AddSchema<Schema, StoredRecord.Create<Name>>>,
'flat'
>

export type CreateFromSchemaDef<
Name extends SomeName,
SchemaDef extends SomeSchemaDef
> = CreateFromStoredRecord<StoredRecord.AddSchemaDef<SchemaDef, StoredRecord.Create<Name>>>
export type CreateFromSchemaDef<Name extends SomeName, SchemaDef extends SomeSchemaDef> = Any.Compute<
CreateFromStoredRecord<StoredRecord.AddSchemaDef<SchemaDef, StoredRecord.Create<Name>>>,
'flat'
>

export type CreateFromStoredRecord<R extends SomeStoredRecord> = RecordController<[R], R>
type CreateFromStoredRecord<R extends SomeStoredRecord> = RecordController<[R], R>

export type GetConstructorInput<V extends SomeStoredRecord> = ApplyDefaults<
V['defaults'],
Expand All @@ -95,16 +97,15 @@ export type RecordController<Rs extends StoredRecords, R extends SomeStoredRecor
codecs: [...string[]]
}
name: R[`name`]
schema: R['schema']
/**
*
* @throws If zod schema violated: bad types, failed validation, throw from a transformer.
*/
update(record: StoredRecord.GetType<R>, changes: Partial<OmitTag<StoredRecord.GetType<R>>>): StoredRecord.GetType<R>
update(record: StoredRecord.GetType<R>, changes: Any.Compute<Partial<OmitTag<StoredRecord.GetType<R>>>>): StoredRecord.GetType<R>
/**
* Decoders for this record. Decoders are used to transform other representations of your record back into a record instance.
*/
from: {
from: Any.Compute<{
/**
* Decode JSON into this record. If it fails for any reason, returns `null`.
*
Expand All @@ -117,21 +118,21 @@ export type RecordController<Rs extends StoredRecords, R extends SomeStoredRecor
* @remarks This is a built in decoder.
*/
jsonOrThrow(value: string): StoredRecord.GetType<R>
} & Decoders<R['codec'], R>
} & Decoders<R['codec'], R>>
// & {
// [I in IndexKeys<V['codec']> as AsString<V['codec'][I]>]: Decoder<V['codec']>,V>
// }
/**
* Encoders for this record. Encoders are used to transform your record into another representation.
*/
to: {
to: Any.Compute<{
/**
* Encode an instance of this record into JSON.
*
* @remarks This is a built in encoder.
*/
json(record: StoredRecord.GetType<R>): string
} & Encoders<R['codec'], R>
} & Encoders<R['codec'], R>>
/**
* Strict predicate/type guard for this record.
*
Expand Down Expand Up @@ -168,15 +169,18 @@ export type RecordController<Rs extends StoredRecords, R extends SomeStoredRecor
/**
* TODO
*/
create(input?: RecordController.GetConstructorInput<R>): StoredRecord.GetType<R>
create(input?: Any.Compute<RecordController.GetConstructorInput<R>>): StoredRecord.GetType<R>
}
: {
/**
* TODO
*/
create(input: RecordController.GetConstructorInput<R>): StoredRecord.GetType<R>
create(input: Any.Compute<RecordController.GetConstructorInput<R>>): StoredRecord.GetType<R>
}) &
R[`extensions`]
R[`extensions`] &
{
schema: R['schema']
}

export type ApplyDefaults<Defaults, Input> = {
[K in keyof Input as K extends keyof Defaults ? never : K]: Input[K]
Expand Down

0 comments on commit 408eb22

Please sign in to comment.