Skip to content

Commit 2ba316c

Browse files
authored
fix: fix Input generic contravariant error with TS 5+ (#298)
1 parent 849dd91 commit 2ba316c

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/core/internal.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const GROUPED_AS_REPLACE_RE = /^(?:\(\?:(.+)\)|(\(?.+\)?))$/
77
const GROUPED_REPLACE_RE = /^(?:\(\?:(.+)\)([?+*]|{[\d,]+})?|(.+))$/
88

99
export interface Input<
10-
in V extends string,
10+
V extends string,
1111
G extends string = never,
1212
C extends (string | undefined)[] = []
1313
> {
@@ -67,22 +67,34 @@ export interface Input<
6767
) => Input<`${V}(?!${Join<MapToValues<I>, '', ''>})`, G, [...C, ...CG]>
6868
/** repeat the previous pattern an exact number of times */
6969
times: {
70-
<N extends number>(number: N): Input<IfUnwrapped<V, `(?:${V}){${N}}`, `${V}{${N}}`>, G, C>
70+
<N extends number, NV extends string = IfUnwrapped<V, `(?:${V}){${N}}`, `${V}{${N}}`>>(
71+
number: N
72+
): Input<NV, G, C>
7173
/** specify that the expression can repeat any number of times, _including none_ */
72-
any: () => Input<IfUnwrapped<V, `(?:${V})*`, `${V}*`>, G, C>
74+
any: <NV extends string = IfUnwrapped<V, `(?:${V})*`, `${V}*`>>() => Input<NV, G, C>
7375
/** specify that the expression must occur at least `N` times */
74-
atLeast: <N extends number>(
76+
atLeast: <
77+
N extends number,
78+
NV extends string = IfUnwrapped<V, `(?:${V}){${N},}`, `${V}{${N},}`>
79+
>(
7580
number: N
76-
) => Input<IfUnwrapped<V, `(?:${V}){${N},}`, `${V}{${N},}`>, G, C>
81+
) => Input<NV, G, C>
7782
/** specify that the expression must occur at most `N` times */
78-
atMost: <N extends number>(
83+
atMost: <
84+
N extends number,
85+
NV extends string = IfUnwrapped<V, `(?:${V}){0,${N}}`, `${V}{0,${N}}`>
86+
>(
7987
number: N
80-
) => Input<IfUnwrapped<V, `(?:${V}){0,${N}}`, `${V}{0,${N}}`>, G, C>
88+
) => Input<NV, G, C>
8189
/** specify a range of times to repeat the previous pattern */
82-
between: <Min extends number, Max extends number>(
90+
between: <
91+
Min extends number,
92+
Max extends number,
93+
NV extends string = IfUnwrapped<V, `(?:${V}){${Min},${Max}}`, `${V}{${Min},${Max}}`>
94+
>(
8395
min: Min,
8496
max: Max
85-
) => Input<IfUnwrapped<V, `(?:${V}){${Min},${Max}}`, `${V}{${Min},${Max}}`>, G, C>
97+
) => Input<NV, G, C>
8698
}
8799
/** this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()`. Alias for `groupedAs` */
88100
as: <K extends string>(
@@ -112,7 +124,8 @@ export interface Input<
112124
lineEnd: () => Input<`${V}$`, G, C>
113125
}
114126
/** this allows you to mark the input so far as optional */
115-
optionally: () => Input<IfUnwrapped<V, `(?:${V})?`, `${V}?`>, G, C>
127+
optionally: <NV extends string = IfUnwrapped<V, `(?:${V})?`, `${V}?`>>() => Input<NV, G, C>
128+
116129
toString: () => string
117130
}
118131

0 commit comments

Comments
 (0)