From 0be9fb3c08b70048c119a092658717baa76f5531 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 18 Jul 2022 12:42:29 +0100 Subject: [PATCH] fix(types): escape generated types for `anyOf` --- src/core/types/sources.ts | 7 ++++--- test/inputs.test.ts | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/core/types/sources.ts b/src/core/types/sources.ts index bc5fe11c..42044842 100644 --- a/src/core/types/sources.ts +++ b/src/core/types/sources.ts @@ -1,9 +1,10 @@ -import { Input } from '../internal' +import type { Input } from '../internal' +import type { GetValue } from './escape' export type InputSource = S | Input export type MapToValues[]> = T extends [infer First, ...infer Rest] - ? First extends InputSource - ? [K, ...MapToValues] + ? First extends InputSource + ? [GetValue, ...MapToValues] : [] : [] diff --git a/test/inputs.test.ts b/test/inputs.test.ts index 093cc5cd..266f0eb6 100644 --- a/test/inputs.test.ts +++ b/test/inputs.test.ts @@ -32,12 +32,12 @@ describe('inputs', () => { expectTypeOf(extractRegExp(input)).toMatchTypeOf<'[^fo\\^\\-]'>() }) it('anyOf', () => { - const values = ['foo', 'bar', 'baz'] as const + const values = ['fo/o', 'bar', 'baz', oneOrMore('this')] as const const input = anyOf(...values) const regexp = new RegExp(input as any) - expect(regexp).toMatchInlineSnapshot('/\\(foo\\|bar\\|baz\\)/') - expectTypeOf(extractRegExp(input)).toMatchTypeOf<'(foo|bar|baz)'>() - for (const value of values) { + expect(regexp).toMatchInlineSnapshot('/\\(fo\\\\/o\\|bar\\|baz\\|\\(this\\)\\+\\)/') + expectTypeOf(extractRegExp(input)).toMatchTypeOf<'(fo\\/o|bar|baz|(this)+)'>() + for (const value of values.slice(0, -1) as string[]) { expect(regexp.test(value)).toBeTruthy() } expect(regexp.test('qux')).toBeFalsy()