Skip to content

Commit 0be9fb3

Browse files
committed
fix(types): escape generated types for anyOf
1 parent 0c7bec7 commit 0be9fb3

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/core/types/sources.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Input } from '../internal'
1+
import type { Input } from '../internal'
2+
import type { GetValue } from './escape'
23

34
export type InputSource<S extends string = never, T extends string = never> = S | Input<S, T>
45
export type MapToValues<T extends InputSource<any, any>[]> = T extends [infer First, ...infer Rest]
5-
? First extends InputSource<infer K>
6-
? [K, ...MapToValues<Rest>]
6+
? First extends InputSource<string>
7+
? [GetValue<First>, ...MapToValues<Rest>]
78
: []
89
: []
910

test/inputs.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ describe('inputs', () => {
3232
expectTypeOf(extractRegExp(input)).toMatchTypeOf<'[^fo\\^\\-]'>()
3333
})
3434
it('anyOf', () => {
35-
const values = ['foo', 'bar', 'baz'] as const
35+
const values = ['fo/o', 'bar', 'baz', oneOrMore('this')] as const
3636
const input = anyOf(...values)
3737
const regexp = new RegExp(input as any)
38-
expect(regexp).toMatchInlineSnapshot('/\\(foo\\|bar\\|baz\\)/')
39-
expectTypeOf(extractRegExp(input)).toMatchTypeOf<'(foo|bar|baz)'>()
40-
for (const value of values) {
38+
expect(regexp).toMatchInlineSnapshot('/\\(fo\\\\/o\\|bar\\|baz\\|\\(this\\)\\+\\)/')
39+
expectTypeOf(extractRegExp(input)).toMatchTypeOf<'(fo\\/o|bar|baz|(this)+)'>()
40+
for (const value of values.slice(0, -1) as string[]) {
4141
expect(regexp.test(value)).toBeTruthy()
4242
}
4343
expect(regexp.test('qux')).toBeFalsy()

0 commit comments

Comments
 (0)