Skip to content

Commit c7645a1

Browse files
authored
Merge pull request #535 from thoom76/patch-2
Add (multidimensional) enum array typing
2 parents 34af5c0 + 46e5f91 commit c7645a1

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/Model.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ type EntityFieldFromType<T extends OneField> = T['type'] extends ArrayConstructo
124124
? EntityFieldFromType<Exclude<T['items'], undefined>>[]
125125
: never
126126

127-
type ArrayItemType<T extends OneField> = T extends {items: OneField} ? EntityFieldFromType<T['items']> : any
127+
type ArrayItemType<T extends OneField> = T extends {items: OneField}
128+
? EntityField<T['items']>
129+
: any
128130
/*
129131
Select the required properties from a model
130132
*/

test/array-items.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {Client, Table} from './utils/init'
1+
import {Client, Entity, Table} from './utils/init'
22
import {ArrayItemsSchema} from './schemas'
3+
import { ColorEnum } from './schemas/arrayItemsSchema'
34

45
// jest.setTimeout(7200 * 1000)
56

@@ -14,6 +15,7 @@ const table = new Table({
1415
const expected = {
1516
id: '1111-2222',
1617
arrayWithTypedItems: [{bar: 'Bar', when: new Date()}],
18+
arrayWithEnumItems: [ColorEnum.blue, ColorEnum.red, ColorEnum.white],
1719
arrayWithoutTypedItems: ['a', '2', 3, new Date()],
1820
}
1921

@@ -34,6 +36,7 @@ test('Create', async () => {
3436
expect(item.arrayWithTypedItems).toBeDefined()
3537
expect(item.arrayWithTypedItems.length).toBe(1)
3638
expect(item.arrayWithTypedItems[0].bar).toBe('Bar')
39+
expect(item.arrayWithEnumItems).toStrictEqual(expected.arrayWithEnumItems)
3740
expect(item.arrayWithoutTypedItems.length).toBe(4)
3841
expect(item.arrayWithoutTypedItems[0]).toBe('a')
3942

@@ -61,3 +64,11 @@ test('Destroy Table', async () => {
6164
await table.deleteTable('DeleteTableForever')
6265
expect(await table.exists()).toBe(false)
6366
})
67+
68+
test('Array with enum items typing', () => {
69+
type ArrayWithEnumItemsType = Entity<typeof ArrayItemsSchema.models.TestModel>['arrayWithEnumItems'];
70+
const validA: ColorEnum[]|undefined = {} as ArrayWithEnumItemsType;
71+
const validB: ArrayWithEnumItemsType = {} as ColorEnum[]|undefined;
72+
// @ts-expect-error
73+
const invalid: ArrayWithEnumItemsType = {} as string[]|undefined;
74+
});

test/schemas/arrayItemsSchema.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Schema to test items property for array type
33
*/
44

5+
export enum ColorEnum {
6+
red = 'red',
7+
white = 'white',
8+
blue = 'blue'
9+
}
10+
511
export default {
612
version: '0.0.1',
713
format: 'onetable:1.1.0',
@@ -26,6 +32,13 @@ export default {
2632
},
2733
},
2834
},
35+
arrayWithEnumItems: {
36+
type: Array,
37+
items: {
38+
type: String,
39+
enum: Object.values(ColorEnum),
40+
},
41+
},
2942
arrayWithoutTypedItems: {type: Array, required: true},
3043
},
3144
} as const,

0 commit comments

Comments
 (0)