Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: fix Input generic contravariant error with TS 5+ #298

Merged
merged 1 commit into from
Apr 15, 2023

Conversation

didavid61202
Copy link
Collaborator

Note

When resolving type inferencing issue (using extractRegExp helper or directly using createRegExp) when implementing new letter.lowercase and letter.uppercase helper inputs in PR #77 we update the generic argument V of interface Input to a contravariant generic arg in PR #79, which in the newest TypeScript 5.0.2 and above cause type error:

Type 'Input<super-V, G, C>' is not assignable to type 'Input<sub-V, G, C>' as implied by variance annotation.Type 'Input<super-V, G, C>' is not assignable to type 'Input<sub-V, G, C>' as implied by variance annotation.

to resolve this issue and #77, we can actually remove the variance annotation and change the position of where the "new value" of V (the built-up RegExp pattern string) of Input interface, this prevent the wrong inferencing of V seen in #77.

Updates

  1. Remove in contravariant annotation to type parameter V of Input interface:
interface Input<
-  in V extends string,
+  V extends string,
  G extends string = never,
  C extends (string | undefined)[] = [] > { ... }
  1. Add NV generic arguments to Input chain functions and resolve the new RegExp pattern string literal type as arg default, for example:
 between: <
      Min extends number,
      Max extends number,
+    NV extends string = IfUnwrapped<V, `(?:${V}){${Min},${Max}}`, `${V}{${Min},${Max}}`>
    >(
      min: Min,
      max: Max
      ) => 
    Input<
-       IfUnwrapped<V, `(?:${V}){${Min},${Max}}`, `${V}{${Min},${Max}}`>, G, C>,
+       NV,
        G,
        C
    >

Related issues

#77

@vercel
Copy link

vercel bot commented Apr 15, 2023

Someone is attempting to deploy a commit to a Personal Account owned by @danielroe on Vercel.

@danielroe first needs to authorize it.

@didavid61202 didavid61202 requested a review from danielroe April 15, 2023 14:27
@codecov-commenter
Copy link

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (849dd91) 100.00% compared to head (f58da5b) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #298   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines          520       533   +13     
  Branches        87        87           
=========================================
+ Hits           520       533   +13     
Impacted Files Coverage Δ
src/core/internal.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@vercel
Copy link

vercel bot commented Apr 15, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
magic-regexp ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 15, 2023 2:31pm

@danielroe danielroe changed the title fix: Input generic contravariant error above typesciprt 5 fix: fix Input generic contravariant error with TS 5+ Apr 15, 2023
@danielroe danielroe merged commit 2ba316c into unjs:main Apr 15, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants