From ba7d3806db1e8922e8b4ff225ab07496ac049243 Mon Sep 17 00:00:00 2001 From: Thomas Smeele Date: Mon, 15 Jul 2024 16:44:20 +0200 Subject: [PATCH 1/6] Update Model.d.ts Make EntityField recursive when 'items' is defined to obtain the right type. --- src/Model.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Model.d.ts b/src/Model.d.ts index 3072572..532ded6 100644 --- a/src/Model.d.ts +++ b/src/Model.d.ts @@ -100,7 +100,9 @@ export type OneSchemaParams = { /* Entity field signature generated from the schema */ -type EntityField = T['enum'] extends readonly EntityFieldFromType[] +type EntityField = T['items'] extends object + ? EntityField[] + : T['enum'] extends readonly EntityFieldFromType[] ? T['enum'][number] : EntityFieldFromType From 804df7a4991d742c633532b1cc49c4dec14206ef Mon Sep 17 00:00:00 2001 From: Thomas Smeele Date: Mon, 15 Jul 2024 17:09:58 +0200 Subject: [PATCH 2/6] Only base the type of the defined type in items when the type is an array type --- src/Model.d.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Model.d.ts b/src/Model.d.ts index 532ded6..cf60979 100644 --- a/src/Model.d.ts +++ b/src/Model.d.ts @@ -100,11 +100,13 @@ export type OneSchemaParams = { /* Entity field signature generated from the schema */ -type EntityField = T['items'] extends object - ? EntityField[] - : T['enum'] extends readonly EntityFieldFromType[] - ? T['enum'][number] - : EntityFieldFromType +type EntityField = T['type'] extends 'array' | ArrayConstructor | ArrayBufferConstructor + ? T['items'] extends object + ? EntityField[] + : EntityFieldFromType + : T['enum'] extends readonly EntityFieldFromType[] + ? T['enum'][number] + : EntityFieldFromType type EntityFieldFromType = T['type'] extends ArrayConstructor | 'array' ? ArrayItemType[] From a4d5f0f7a310395cdb321aafeabde1ac9c72917f Mon Sep 17 00:00:00 2001 From: Thomas Smeele Date: Mon, 15 Jul 2024 15:42:00 +0000 Subject: [PATCH 3/6] Make PR cleaner --- src/Model.d.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Model.d.ts b/src/Model.d.ts index cf60979..eddd773 100644 --- a/src/Model.d.ts +++ b/src/Model.d.ts @@ -100,13 +100,9 @@ export type OneSchemaParams = { /* Entity field signature generated from the schema */ -type EntityField = T['type'] extends 'array' | ArrayConstructor | ArrayBufferConstructor - ? T['items'] extends object - ? EntityField[] - : EntityFieldFromType - : T['enum'] extends readonly EntityFieldFromType[] - ? T['enum'][number] - : EntityFieldFromType +type EntityField = T['enum'] extends readonly EntityFieldFromType[] + ? T['enum'][number] + : EntityFieldFromType type EntityFieldFromType = T['type'] extends ArrayConstructor | 'array' ? ArrayItemType[] @@ -128,7 +124,9 @@ type EntityFieldFromType = T['type'] extends ArrayConstructo ? EntityFieldFromType>[] : never -type ArrayItemType = T extends {items: OneField} ? EntityFieldFromType : any +type ArrayItemType = T extends {items: OneField} + ? EntityField + : EntityFieldFromType /* Select the required properties from a model */ From 79d1adfbad2bbe4b35105acf6fc88e85fff34472 Mon Sep 17 00:00:00 2001 From: Thomas Smeele Date: Tue, 16 Jul 2024 20:51:52 +0200 Subject: [PATCH 4/6] Make typescript typing happy again :) --- src/Model.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model.d.ts b/src/Model.d.ts index eddd773..2ac5590 100644 --- a/src/Model.d.ts +++ b/src/Model.d.ts @@ -126,7 +126,7 @@ type EntityFieldFromType = T['type'] extends ArrayConstructo type ArrayItemType = T extends {items: OneField} ? EntityField - : EntityFieldFromType + : any /* Select the required properties from a model */ From 8fd3461a5ffc8a09114601f70721f0c95d8628d0 Mon Sep 17 00:00:00 2001 From: Thomas Smeele Date: Wed, 17 Jul 2024 10:23:16 +0200 Subject: [PATCH 5/6] Write some tests for the enum array --- test/array-items.ts | 13 ++++++++++++- test/schemas/arrayItemsSchema.ts | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/test/array-items.ts b/test/array-items.ts index b5d0ee7..5d8fe8d 100644 --- a/test/array-items.ts +++ b/test/array-items.ts @@ -1,5 +1,6 @@ -import {Client, Table} from './utils/init' +import {Client, Entity, Table} from './utils/init' import {ArrayItemsSchema} from './schemas' +import { ColorEnum } from './schemas/arrayItemsSchema' // jest.setTimeout(7200 * 1000) @@ -14,6 +15,7 @@ const table = new Table({ const expected = { id: '1111-2222', arrayWithTypedItems: [{bar: 'Bar', when: new Date()}], + arrayWithEnumItems: [ColorEnum.blue, ColorEnum.red, ColorEnum.white], arrayWithoutTypedItems: ['a', '2', 3, new Date()], } @@ -34,6 +36,7 @@ test('Create', async () => { expect(item.arrayWithTypedItems).toBeDefined() expect(item.arrayWithTypedItems.length).toBe(1) expect(item.arrayWithTypedItems[0].bar).toBe('Bar') + expect(item.arrayWithEnumItems).toBe(expected.arrayWithEnumItems) expect(item.arrayWithoutTypedItems.length).toBe(4) expect(item.arrayWithoutTypedItems[0]).toBe('a') @@ -61,3 +64,11 @@ test('Destroy Table', async () => { await table.deleteTable('DeleteTableForever') expect(await table.exists()).toBe(false) }) + +test('Array with enum items typing', () => { + type ArrayWithEnumItemsType = Entity['arrayWithEnumItems']; + const validA: ColorEnum[]|undefined = {} as ArrayWithEnumItemsType; + const validB: ArrayWithEnumItemsType = {} as ColorEnum[]|undefined; + // @ts-expect-error + const invalid: ArrayWithEnumItemsType = {} as string[]|undefined; +}); \ No newline at end of file diff --git a/test/schemas/arrayItemsSchema.ts b/test/schemas/arrayItemsSchema.ts index 516e67e..ddd954e 100644 --- a/test/schemas/arrayItemsSchema.ts +++ b/test/schemas/arrayItemsSchema.ts @@ -2,6 +2,12 @@ Schema to test items property for array type */ +export enum ColorEnum { + red = 'red', + white = 'white', + blue = 'blue' +} + export default { version: '0.0.1', format: 'onetable:1.1.0', @@ -26,6 +32,13 @@ export default { }, }, }, + arrayWithEnumItems: { + type: Array, + items: { + type: String, + enum: Object.values(ColorEnum), + }, + }, arrayWithoutTypedItems: {type: Array, required: true}, }, } as const, From 46e5f91fe330f43372c9728ce2dbac5c1ebb0714 Mon Sep 17 00:00:00 2001 From: Thomas Smeele Date: Wed, 17 Jul 2024 12:19:46 +0200 Subject: [PATCH 6/6] Use strictEqual --- test/array-items.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/array-items.ts b/test/array-items.ts index 5d8fe8d..d99d865 100644 --- a/test/array-items.ts +++ b/test/array-items.ts @@ -36,7 +36,7 @@ test('Create', async () => { expect(item.arrayWithTypedItems).toBeDefined() expect(item.arrayWithTypedItems.length).toBe(1) expect(item.arrayWithTypedItems[0].bar).toBe('Bar') - expect(item.arrayWithEnumItems).toBe(expected.arrayWithEnumItems) + expect(item.arrayWithEnumItems).toStrictEqual(expected.arrayWithEnumItems) expect(item.arrayWithoutTypedItems.length).toBe(4) expect(item.arrayWithoutTypedItems[0]).toBe('a')