Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

IfAny circular constraint in TS 5.4+ #846

Open
kkmuffme opened this issue Mar 27, 2024 · 2 comments
Open

IfAny circular constraint in TS 5.4+ #846

kkmuffme opened this issue Mar 27, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@kkmuffme
Copy link
Contributor

kkmuffme commented Mar 27, 2024

Line 3 reports Type parameter 'T' has a circular constraint. (2313) since TS 5.4 which can be fixed with NoInfer

import type {IfAny} from "type-fest";

function foo<T extends IfAny<T, never, string>>( arg: T ): string;

function fooN<T extends IfAny<NoInfer<T>, never, string>>( arg: T ): string;

Playground

Is this something that can be fixed by IfAny internally to avoid having to change 100s of places where it reports this error since TS 5.4 in our codebase?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

Yeah, it does seem like we can just do:

-type IsAny<T> = 0 extends 1 & T ? true : false;
+type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;

@tommy-mitchell Thoughts?

@voxpelli
Copy link
Collaborator

Also needs tests that catches the error, so yet another one that highlights #837

Haroenv added a commit to algolia/instantsearch that referenced this issue Jun 20, 2024
I can't manage to reproduce this locally, even if i update everything to 5.4.5, but in a fresh Next.js example (like  https://codesandbox.io/s/47fy7c) `hitsPerPage` isn't accepted to Configure (because PlainSearchParameters is any)

This is I believe linked to microsoft/TypeScript#56004, maybe also to sindresorhus/type-fest#846 (although the case seems different).

Essentially before this change the types like `ClientLiteV5` were evaluated to `any` instead of `unknown`, poisoning the entire type and turning everything into any.

The solution is to first check if `typeof AlgoliaSearchLite` is any, and if it is any we don't even evaluate the rest of teh type. Of course if it isn't any it works correctly for v5 still.

Again, unfortunately this isn't reproducible in this repo, even when every typescript version is updated to 5.4.5, but at least the error will be fixed

fixes #5989
Haroenv added a commit to algolia/instantsearch that referenced this issue Jun 20, 2024
)

I can't manage to reproduce this locally, even if i update everything to 5.4.5, but in a fresh Next.js example (like  https://codesandbox.io/s/47fy7c) `hitsPerPage` isn't accepted to Configure (because PlainSearchParameters is any)

This is I believe linked to microsoft/TypeScript#56004, maybe also to sindresorhus/type-fest#846 (although the case seems different).

Essentially before this change the types like `ClientLiteV5` were evaluated to `any` instead of `unknown`, poisoning the entire type and turning everything into any.

The solution is to first check if `typeof AlgoliaSearchLite` is any, and if it is any we don't even evaluate the rest of teh type. Of course if it isn't any it works correctly for v5 still.

Again, unfortunately this isn't reproducible in this repo, even when every typescript version is updated to 5.4.5, but at least the error will be fixed

fixes #5989
@fregante fregante added the bug Something isn't working label Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants