Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (multidimensional) enum array typing #535

Merged
merged 6 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ type EntityFieldFromType<T extends OneField> = T['type'] extends ArrayConstructo
? EntityFieldFromType<Exclude<T['items'], undefined>>[]
: never

type ArrayItemType<T extends OneField> = T extends {items: OneField} ? EntityFieldFromType<T['items']> : any
type ArrayItemType<T extends OneField> = T extends {items: OneField}
? EntityField<T['items']>
: any
/*
Select the required properties from a model
*/
Expand Down
13 changes: 12 additions & 1 deletion test/array-items.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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()],
}

Expand All @@ -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')

Expand Down Expand Up @@ -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<typeof ArrayItemsSchema.models.TestModel>['arrayWithEnumItems'];
const validA: ColorEnum[]|undefined = {} as ArrayWithEnumItemsType;
const validB: ArrayWithEnumItemsType = {} as ColorEnum[]|undefined;
// @ts-expect-error
const invalid: ArrayWithEnumItemsType = {} as string[]|undefined;
});
13 changes: 13 additions & 0 deletions test/schemas/arrayItemsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -26,6 +32,13 @@ export default {
},
},
},
arrayWithEnumItems: {
type: Array,
items: {
type: String,
enum: Object.values(ColorEnum),
},
},
arrayWithoutTypedItems: {type: Array, required: true},
},
} as const,
Expand Down