diff --git a/package.json b/package.json index 1a24cd2..450abe0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-expression-eval", - "version": "3.2.0", + "version": "3.3.0", "description": "evaluate a json described boolean expression using dynamic functions", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/example/example.ts b/src/example/example.ts index c99f687..9106836 100644 --- a/src/example/example.ts +++ b/src/example/example.ts @@ -102,13 +102,14 @@ run(expression, context); const evaluator = getEvaluator(expression); -const expressionParts: GetExpressionParts = { +const expressionParts: GetExpressionParts = { nested: { value: { isArray: false, isFunction: false, propertyPath: 'nested.value', type: 'number', + description: 'my desc', }, nested2: { value: { @@ -116,6 +117,7 @@ const expressionParts: GetExpressionParts = { isFunction: false, propertyPath: 'nested.nested2.value', type: 'number', + description: 'my desc', }, }, }, @@ -124,23 +126,27 @@ const expressionParts: GetExpressionParts = { isFunction: false, propertyPath: 'times', type: 'number', + description: 'my desc', }, userId: { isArray: false, isFunction: false, propertyPath: 'userId', type: 'string', + description: 'my desc', }, maxCount: { isArray: false, isFunction: true, propertyPath: 'maxCount', type: 'number', + description: 'my desc', }, user: { isArray: false, isFunction: true, propertyPath: 'user', type: 'string', + description: 'my desc', }, } as const; diff --git a/src/expressionParts.ts b/src/expressionParts.ts index 2569dd8..ad235af 100644 --- a/src/expressionParts.ts +++ b/src/expressionParts.ts @@ -12,8 +12,8 @@ interface ExpressionFunctionPart, isFunction: true; } -type ExpressionFunctionParts> = { - [K in keyof F]: ExpressionFunctionPart +type ExpressionFunctionParts, Extra extends object> = { + [K in keyof F]: ExpressionFunctionPart & Extra; } interface ExpressionContextPart> { @@ -25,15 +25,15 @@ interface ExpressionContextPart> = { +type _ExpressionContextParts, Extra extends object> = { [k in U.Select]: - C[k] extends Primitive ? ExpressionContextPart + C[k] extends Primitive ? ExpressionContextPart & Extra : C[k] extends Array ? never - : _ExpressionContextParts; + : _ExpressionContextParts; } -type ExpressionContextParts = _ExpressionContextParts; +type ExpressionContextParts = _ExpressionContextParts; -export type ExpressionParts> = - ExpressionFunctionParts - & ExpressionContextParts; +export type ExpressionParts, Extra extends object = {}> = + ExpressionFunctionParts + & ExpressionContextParts; diff --git a/src/index.ts b/src/index.ts index 40b0ef3..6c1abb2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,5 +23,5 @@ export class ExpressionEval> { } -export type GetExpressionParts> = - E extends ExpressionEval ? ExpressionParts : never; +export type GetExpressionParts, Extra extends object = {}> = + E extends ExpressionEval ? ExpressionParts : never; diff --git a/src/test/expressionParts.test-d.ts b/src/test/expressionParts.test-d.ts index ed88619..081e401 100644 --- a/src/test/expressionParts.test-d.ts +++ b/src/test/expressionParts.test-d.ts @@ -97,6 +97,94 @@ const expected = { }, } as const; +type ResultExtended = + O.Readonly, Any.Key, 'deep'>; + +const expectedExtended = { + nested: { + value: { + isArray: false, + isFunction: false, + propertyPath: 'nested.value', + type: 'number', + description: 'my desc' as string, + }, + nested2: { + value: { + isArray: false, + isFunction: false, + propertyPath: 'nested.nested2.value', + type: 'string', + description: 'my desc' as string, + }, + }, + }, + str: { + isArray: false, + isFunction: false, + propertyPath: 'str', + type: 'string', + description: 'my desc' as string, + }, + strFn: { + isArray: false, + isFunction: true, + propertyPath: 'strFn', + type: 'string', + description: 'my desc' as string, + }, + strArrFn: { + isArray: true, + isFunction: true, + propertyPath: 'strArrFn', + type: 'string', + description: 'my desc' as string, + }, + num: { + isArray: false, + isFunction: false, + propertyPath: 'num', + type: 'number', + description: 'my desc' as string, + }, + numFn: { + isArray: false, + isFunction: true, + propertyPath: 'numFn', + type: 'number', + description: 'my desc' as string, + }, + numArrFn: { + isArray: true, + isFunction: true, + propertyPath: 'numArrFn', + type: 'number', + description: 'my desc' as string, + }, + bool: { + isArray: false, + isFunction: false, + propertyPath: 'bool', + type: 'boolean', + description: 'my desc' as string, + }, + boolFn: { + isArray: false, + isFunction: true, + propertyPath: 'boolFn', + type: 'boolean', + description: 'my desc' as string, + }, + boolArrFn: { + isArray: true, + isFunction: true, + propertyPath: 'boolArrFn', + type: 'boolean', + description: 'my desc' as string, + }, +} as const; + Test.checks([ Test.check(), + Test.check(), ]);