From c0ccd19a4bc5593bce15c9b2c5298c2079c5e216 Mon Sep 17 00:00:00 2001 From: Duncan Beevers Date: Wed, 6 Nov 2024 10:10:39 -0800 Subject: [PATCH] feat: Support readonly options for Types.string and Types.number --- src/__tests__/types.test.ts | 22 +++++++++++++++++----- src/types.ts | 4 ++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/__tests__/types.test.ts b/src/__tests__/types.test.ts index 334af9b5d..071d7176b 100644 --- a/src/__tests__/types.test.ts +++ b/src/__tests__/types.test.ts @@ -155,12 +155,23 @@ describe('types', () => { expectType(undefined); }); - test('options', () => { - types.number({ + test('options, range and modulo', () => { + const options = { maximum: 9, minimum: 2, multipleOf: 2, - }); + } as const; + types.number(options); + + // @ts-expect-error invalid option + types.number({ maxLength: 1 }); + }); + + test('options, enum', () => { + const options = { + enum: [1, 2, 3], + } as const; + types.number(options); // @ts-expect-error invalid option types.number({ maxLength: 1 }); @@ -306,11 +317,12 @@ describe('types', () => { }); test('options', () => { - types.string({ + const options = { enum: ['foo', 'bar'], maxLength: 9, minLength: 1, - }); + } as const; + types.string(options); // @ts-expect-error invalid option types.string({ maximum: 1 }); diff --git a/src/types.ts b/src/types.ts index 7a98543a4..0a36e616a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,7 +18,7 @@ interface ArrayOptions extends GenericOptions { } interface NumberOptions extends GenericOptions { - enum?: number[]; + enum?: readonly number[]; exclusiveMaximum?: boolean; exclusiveMinimum?: boolean; maximum?: number; @@ -35,7 +35,7 @@ interface ObjectOptions extends GenericOptions { } interface StringOptions extends GenericOptions { - enum?: string[]; + enum?: readonly string[]; maxLength?: number; minLength?: number; pattern?: string;