diff --git a/packages/core/strapi/lib/types/__tests__/definitions/utils/array.d.ts b/packages/core/strapi/lib/types/__tests__/definitions/utils/array.d.ts index 9ced06fe82..04cbaf1550 100644 --- a/packages/core/strapi/lib/types/__tests__/definitions/utils/array.d.ts +++ b/packages/core/strapi/lib/types/__tests__/definitions/utils/array.d.ts @@ -1,4 +1,4 @@ -import { Utils } from '@strapi/strapi'; +import type { Utils } from '@strapi/strapi'; type Obj = { foo: 'bar'; @@ -12,7 +12,13 @@ type Obj = { type StringValues = Utils.Array.Values<['foo', 'bar', 'baz']>; type NumberValues = Utils.Array.Values<[1, 2, 3]>; type BoolValues = Utils.Array.Values<[true, false, true]>; +type TrueBoolLiteralValues = Utils.Array.Values<[true, true, true]>; +type FalseBoolLiteralValues = Utils.Array.Values<[false, false, false]>; type ObjectValues = Utils.Array.Values<[Obj, { prop1: true; prop2: false }]>; +type MixedValues = Utils.Array.Values<[Obj, 1, 'foo', true]>; +type ContainsNever = Utils.Array.Values<[never, Obj, 1, 'foo', true]>; + +// TODO move this to tuple utils // Is Empty @@ -28,7 +34,11 @@ export { StringValues, NumberValues, BoolValues, + TrueBoolLiteralValues, + FalseBoolLiteralValues, ObjectValues, + MixedValues, + ContainsNever, IsEmptyWithEmptyTuple, IsEmptyWithNotEmptyTuple, IsNotEmptyWithNotEmptyTuple, diff --git a/packages/core/strapi/lib/types/__tests__/definitions/utils/expression.d.ts b/packages/core/strapi/lib/types/__tests__/definitions/utils/expression.d.ts index 6657d43c84..90daa2a9f5 100644 --- a/packages/core/strapi/lib/types/__tests__/definitions/utils/expression.d.ts +++ b/packages/core/strapi/lib/types/__tests__/definitions/utils/expression.d.ts @@ -1,5 +1,4 @@ import type { Utils } from '@strapi/strapi'; -import { Array } from '../../../../services/entity-service/types/params/filters/operators'; // IsNever type IsNeverGivenNever = Utils.Expression.IsNever; @@ -37,6 +36,10 @@ type IsStrictEqualGivenEqualLiterals = Utils.Expression.StrictEqual<1, 1>; type IsStrictEqualGivenEqualTypes = Utils.Expression.StrictEqual; type IsStrictEqualGivenDifferentLiterals = Utils.Expression.StrictEqual<1, 2>; type IsStrictEqualGivenDifferentTypes = Utils.Expression.StrictEqual; +type IsStrictEqualGivenStringAndStringLiteral = Utils.Expression.StrictEqual; +type IsStrictEqualGivenStringLiteralAndString = Utils.Expression.StrictEqual<'hello', string>; +type IsStrictEqualGivenNumberAndNumberLiteral = Utils.Expression.StrictEqual; +type IsStrictEqualGivenNumberLiteralAndNumber = Utils.Expression.StrictEqual<1, number>; // Extends type StringExtendsString = Utils.Expression.Extends; @@ -86,6 +89,45 @@ type ArrayNotExtendsArray = Utils.Expression.DoesNotExtends, Array type TupleNotExtendsArray = Utils.Expression.DoesNotExtends<[string], Array>; type StringArrayNotExtendsArray = Utils.Expression.DoesNotExtends>; +// If +type IfTrue = Utils.Expression.If; +type IfFalse = Utils.Expression.If; +type IfBoolean = Utils.Expression.If; +type IfNumber = Utils.Expression.If; +type IfString = Utils.Expression.If; +type IfObject = Utils.Expression.If; +type IfUnknown = Utils.Expression.If; +type IfAny = Utils.Expression.If; +type IfNever = Utils.Expression.If; +type IfStringLiteral = Utils.Expression.If<'test', true, false>; +type IfNumberLiteral = Utils.Expression.If<10, true, false>; +type IfObjectLiteral = Utils.Expression.If<{ test: 1 }, true, false>; +type IfTuple = Utils.Expression.If<[1, 2, 3], true, false>; +type IfArray = Utils.Expression.If, true, false>; +type IfStringArray = Utils.Expression.If; +type IfTupleArray = Utils.Expression.If<[string, number], true, false>; +type IfUnion = Utils.Expression.If; +type IfIntersection = Utils.Expression.If; +type IfFunction = Utils.Expression.If<() => void, true, false>; +type IfClass = Utils.Expression.If void, true, false>; +type IfVoid = Utils.Expression.If; +type IfNull = Utils.Expression.If; +type IfUndefined = Utils.Expression.If; +type IfWithStringReturnType = Utils.Expression.If; +type IfWithNumberReturnType = Utils.Expression.If; +type IfWithBooleanReturnType = Utils.Expression.If; +type IfWithObjectReturnType = Utils.Expression.If; +type IfWithTupleReturnType = Utils.Expression.If; +// TODO Check this type +type IfWithArrayReturnType = Utils.Expression.If, Array>; +type IfWithUnionReturnType = Utils.Expression.If; +type IfWithVoidReturnType = Utils.Expression.If; +type IfWithNullReturnType = Utils.Expression.If; +type IfWithUndefinedReturnType = Utils.Expression.If; +type IfWithNeverReturnType = Utils.Expression.If; +type IfWithUnknownReturnType = Utils.Expression.If; +type IfWithAnyReturnType = Utils.Expression.If; + export { // IsNever IsNeverGivenNever, @@ -103,6 +145,10 @@ export { IsStrictEqualGivenEqualTypes, IsStrictEqualGivenDifferentLiterals, IsStrictEqualGivenDifferentTypes, + IsStrictEqualGivenStringLiteralAndString, + IsStrictEqualGivenStringAndStringLiteral, + IsStrictEqualGivenNumberAndNumberLiteral, + IsStrictEqualGivenNumberLiteralAndNumber, // IsTrue IsTrueGivenTrue, IsTrueGivenFalse, @@ -165,4 +211,41 @@ export { ArrayNotExtendsArray, TupleNotExtendsArray, StringArrayNotExtendsArray, + // If + IfTrue, + IfFalse, + IfBoolean, + IfNumber, + IfString, + IfObject, + IfUnknown, + IfAny, + IfNever, + IfStringLiteral, + IfNumberLiteral, + IfObjectLiteral, + IfTuple, + IfArray, + IfStringArray, + IfTupleArray, + IfUnion, + IfIntersection, + IfFunction, + IfClass, + IfVoid, + IfNull, + IfUndefined, + IfWithStringReturnType, + IfWithNumberReturnType, + IfWithBooleanReturnType, + IfWithObjectReturnType, + IfWithTupleReturnType, + IfWithArrayReturnType, + IfWithUnionReturnType, + IfWithVoidReturnType, + IfWithNullReturnType, + IfWithUndefinedReturnType, + IfWithNeverReturnType, + IfWithUnknownReturnType, + IfWithAnyReturnType, }; diff --git a/packages/core/strapi/lib/types/__tests__/utils/array.test.js b/packages/core/strapi/lib/types/__tests__/utils/array.test.js index 16ef08dd27..8a626a7d53 100644 --- a/packages/core/strapi/lib/types/__tests__/utils/array.test.js +++ b/packages/core/strapi/lib/types/__tests__/utils/array.test.js @@ -25,12 +25,37 @@ describe('Utils.Array', () => { ]); }); + test('Mixed Values', () => { + const expectedResultType = [ + t.anonymousObject({ + properties: { + foo: t.stringLiteral('bar'), + baz: t.booleanLiteral(false), + prop: t.anonymousObject({ + properties: { + foo: t.stringLiteral('bar'), + bar: t.stringLiteral('foo'), + }, + }), + }, + }), + t.numberLiteral(1), + t.stringLiteral('foo'), + t.booleanLiteral(true), + ]; + type('MixedValues').isUnion(expectedResultType); + // The result type should not contain the type 'never' + type('ContainsNever').isUnion(expectedResultType); + }); + test('Number Values', () => { type('NumberValues').isUnion([t.numberLiteral(1), t.numberLiteral(2), t.numberLiteral(3)]); }); test('Bool Values', () => { type('BoolValues').isBoolean(); + type('TrueBoolLiteralValues').isBooleanLiteral(true); + type('FalseBoolLiteralValues').isBooleanLiteral(false); }); test('Object Values', () => { diff --git a/packages/core/strapi/lib/types/__tests__/utils/expression.test.js b/packages/core/strapi/lib/types/__tests__/utils/expression.test.js index c8b60fc5c9..0f52c94b80 100644 --- a/packages/core/strapi/lib/types/__tests__/utils/expression.test.js +++ b/packages/core/strapi/lib/types/__tests__/utils/expression.test.js @@ -1,6 +1,7 @@ 'use strict'; const path = require('path'); +const { t } = require('ts-zen'); const { createTypeSelector } = require('../test.utils'); @@ -56,6 +57,10 @@ describe('Utils.Expression', () => { type('IsStrictEqualGivenEqualTypes').isBooleanLiteral(true); type('IsStrictEqualGivenDifferentLiterals').isBooleanLiteral(false); type('IsStrictEqualGivenDifferentTypes').isBooleanLiteral(false); + type('IsStrictEqualGivenStringAndStringLiteral').isBooleanLiteral(false); + type('IsStrictEqualGivenStringLiteralAndString').isBooleanLiteral(false); + type('IsStrictEqualGivenNumberAndNumberLiteral').isBooleanLiteral(false); + type('IsStrictEqualGivenNumberLiteralAndNumber').isBooleanLiteral(false); }); test('Extends', () => { @@ -113,4 +118,47 @@ describe('Utils.Expression', () => { type('TupleNotExtendsArray').isBooleanLiteral(false); type('StringArrayNotExtendsArray').isBooleanLiteral(false); }); + + test('If', () => { + type('IfTrue').isBooleanLiteral(true); + type('IfFalse').isBooleanLiteral(false); + type('IfBoolean').isBooleanLiteral(false); + type('IfNumber').isBooleanLiteral(false); + type('IfString').isBooleanLiteral(false); + type('IfObject').isBooleanLiteral(false); + type('IfUnknown').isBooleanLiteral(false); + type('IfAny').isBooleanLiteral(true); + type('IfNever').isBooleanLiteral(true); + type('IfStringLiteral').isBooleanLiteral(false); + type('IfTuple').isBooleanLiteral(false); + type('IfArray').isBooleanLiteral(false); + type('IfStringArray').isBooleanLiteral(false); + type('IfTupleArray').isBooleanLiteral(false); + type('IfUnion').isBooleanLiteral(false); + type('IfIntersection').isBooleanLiteral(true); + type('IfFunction').isBooleanLiteral(false); + type('IfClass').isBooleanLiteral(false); + type('IfVoid').isBooleanLiteral(false); + type('IfNull').isBooleanLiteral(false); + type('IfUndefined').isBooleanLiteral(false); + type('IfWithStringReturnType').isStringLiteral('test'); + type('IfWithNumberReturnType').isNumberLiteral(1); + type('IfWithBooleanReturnType').isBooleanLiteral(true); + type('IfWithObjectReturnType').isAnonymousObject({ + properties: { foo: t.numberLiteral(1) }, + }); + type('IfWithTupleReturnType').isTuple([ + t.numberLiteral(1), + t.numberLiteral(2), + t.numberLiteral(3), + ]); + type('IfWithArrayReturnType').isArray(t.string()); + type('IfWithUnionReturnType').isUnion([t.string(), t.number()]); + type('IfWithVoidReturnType').isVoid(); + type('IfWithNullReturnType').isNull(); + type('IfWithUndefinedReturnType').isUndefined(); + type('IfWithNeverReturnType').isNever(); + type('IfWithUnknownReturnType').isUnknown(); + type('IfWithAnyReturnType').isAny(); + }); });