Skip to content

Commit

Permalink
fix(types): escape generated types for anyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 18, 2022
1 parent 0c7bec7 commit 0be9fb3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions 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 extends string = never, T extends string = never> = S | Input<S, T>
export type MapToValues<T extends InputSource<any, any>[]> = T extends [infer First, ...infer Rest]
? First extends InputSource<infer K>
? [K, ...MapToValues<Rest>]
? First extends InputSource<string>
? [GetValue<First>, ...MapToValues<Rest>]
: []
: []

Expand Down
8 changes: 4 additions & 4 deletions test/inputs.test.ts
Expand Up @@ -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()
Expand Down

0 comments on commit 0be9fb3

Please sign in to comment.