diff --git a/src/Model.d.ts b/src/Model.d.ts index 3072572..2ac5590 100644 --- a/src/Model.d.ts +++ b/src/Model.d.ts @@ -124,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 + : any /* Select the required properties from a model */ diff --git a/test/array-items.ts b/test/array-items.ts index b5d0ee7..d99d865 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).toStrictEqual(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,