diff --git a/docs/typescript-examples/object.ts b/docs/typescript-examples/object.ts index d5ed514..ac7bfc1 100644 --- a/docs/typescript-examples/object.ts +++ b/docs/typescript-examples/object.ts @@ -96,7 +96,7 @@ const block2 = de.http({ const block2Func = de.func({ // eslint-disable-next-line @typescript-eslint/no-unused-vars - block: ({ params }: { params: InferParamsInFromBlock & { p1: number } }) => block2, + block: ({ params }: { params: InferParamsInFromBlock & { p1: number } }) => block2, // block: () => block2, options: { after: ({ result }) => { @@ -111,11 +111,8 @@ const block2Func = de.func({ de.run(block2Func, { params: { - id1: '67890', p1: 1, - payload: { - card: {}, - }, + id2: 578923, }, }) .then((result) => { @@ -166,12 +163,8 @@ const block3 = de.object({ de.run(block3, { params: { - id1: '12345', id2: 67890, p1: 1, - payload: { - card: {}, - }, }, }) .then((result) => { @@ -230,12 +223,8 @@ const block5 = block3.extend({ de.run(block4, { params: { - id1: '12345', id2: 67890, p1: 1, - payload: { - card: {}, - }, }, }) .then((result) => { @@ -244,12 +233,8 @@ de.run(block4, { de.run(block5, { params: { - id1: '12345', id2: 67890, p1: 1, - payload: { - card: {}, - }, }, }) .then((result) => { diff --git a/lib/functionBlock.ts b/lib/functionBlock.ts index 29234b7..c2b82bb 100644 --- a/lib/functionBlock.ts +++ b/lib/functionBlock.ts @@ -2,10 +2,21 @@ import BaseBlock from './block'; import type { DepAccessor, DescriptBlockDeps } from './depsDomain'; import DepsDomain, { createDepAccessor } from './depsDomain'; import { createError, ERROR_ID } from './error'; -import type { BlockResultOut, DescriptBlockOptions, InferParamsOutFromBlock } from './types'; +import type { BlockResultOut, DescriptBlockOptions, DescriptParamsError, InferParamsOutFromBlock } from './types'; import type ContextClass from './context'; import type Cancel from './cancel'; +type NestedBlockParamsConstraint = + [ unknown ] extends [ Params ] + ? T + : T extends BaseBlock + ? unknown extends BParams + ? T + : [ Params ] extends [ BParams ] + ? T + : DescriptParamsError + : T; + export type FunctionBlockDefinition< Context, Params, @@ -18,7 +29,7 @@ export type FunctionBlockDefinition< generateId: DepsDomain['generateId']; cancel: Cancel; blockCancel: Cancel; -}) => Promise | BlockResult; +}) => Promise> | NestedBlockParamsConstraint; class FunctionBlock< Context, diff --git a/lib/index.ts b/lib/index.ts index b87505a..6ea1fa1 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -68,7 +68,7 @@ const array = function< Params = GetArrayBlockParams, >({ block, options }: { block: ArrayBlockDefinition; - options?: DescriptBlockOptions; + options?: DescriptBlockOptions, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>; }) { return new ArrayBlock({ block, options }); }; @@ -85,7 +85,7 @@ const object = function< Params = GetObjectBlockParams, >({ block, options }: { block?: ObjectBlockDefinition; - options?: DescriptBlockOptions; + options?: DescriptBlockOptions, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>; } = {}) { return new ObjectBlock({ block, options }); }; @@ -121,7 +121,7 @@ const first = function< Params = GetFirstBlockParams, >({ block, options }: { block: FirstBlockDefinition; - options?: DescriptBlockOptions; + options?: DescriptBlockOptions, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>; }) { return new FirstBlock({ block, options }); }; @@ -138,7 +138,7 @@ const pipe = function< Params = GetPipeBlockParams, >({ block, options }: { block: PipeBlockDefinition; - options?: DescriptBlockOptions; + options?: DescriptBlockOptions, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>; }) { return new PipeBlock({ block, options }); }; diff --git a/lib/types.ts b/lib/types.ts index 57aea6c..47124a7 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -41,6 +41,13 @@ export type NonNullableObject> = { [P in keyof T]: Exclude; }; +export type DescriptParamsError = { + readonly __descriptError: 'NESTED_BLOCK_PARAMS_INCOMPATIBLE'; + readonly __requiredParams: Required; + readonly __availableParams: Available; + readonly __fix: 'Add options.params to transform parent params into the required shape'; +}; + export type DescriptJSON = boolean | number |