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

ReadonlyDeep does not work for objects with call signatures #337

Closed
marekdedic opened this issue Dec 28, 2021 · 2 comments
Closed

ReadonlyDeep does not work for objects with call signatures #337

marekdedic opened this issue Dec 28, 2021 · 2 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@marekdedic
Copy link

marekdedic commented Dec 28, 2021

Hi,
I have recently stumbled upon an issue with the ReadonlyDeep<> type. I have a type which has both members as well as call signatures (in my case the JQueryStatic type from @types/jquery). When I tried to create ReadonlyDeep<JQueryStatic>, what I got back was just JQueryStatic - no change in type...

Eventually, I tracked it down to the definition:

export type ReadonlyDeep<T> = T extends Primitive | ((...arguments: any[]) => unknown)
? T
: T extends ReadonlyMap<infer KeyType, infer ValueType>
? ReadonlyMapDeep<KeyType, ValueType>
: T extends ReadonlySet<infer ItemType>
? ReadonlySetDeep<ItemType>
: T extends object
? ReadonlyObjectDeep<T>
: unknown;

In this case, because the type has a call signature, T extends Primitive | ((...arguments: any[]) => unknown) evaluates to true and therefore what you get back is the original type...

Trying to fix the issue

I tried to fix the issue by changing the first check to be just T extends Primitive, but then the type would lose all of its call signatures, as it would return ReadonlyObjectDeep<>...

So I tried changing ReadonlyObjectDeep from

type ReadonlyObjectDeep<ObjectType extends object> = {
readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>
};

to

type ReadonlyObjectDeep<ObjectType extends object> = {
    readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>
    (...parameters: Parameters<ObjectType>): ReturnType<ObjectType>
};

but that just got me TS7061: A mapped type may not declare properties or methods.

Is there any way to fix this? Also, as far as I know TS objects have members, call signatures, construct signatures and index infos - the current ReadonlyObjectDeep does only members and index infos, I stumbled here upon call signatures, but construct signatures should probably be solved as well...

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 sindresorhus added bug Something isn't working help wanted Extra attention is needed labels Jan 3, 2022
@sindresorhus sindresorhus changed the title ReadonlyDeep doens't work for objects with call signatures ReadonlyDeep does not work for objects with call signatures Jan 3, 2022
RebeccaStevens added a commit to RebeccaStevens/type-fest that referenced this issue Jan 29, 2022
RebeccaStevens added a commit to RebeccaStevens/type-fest that referenced this issue Jan 29, 2022
RebeccaStevens added a commit to RebeccaStevens/type-fest that referenced this issue Jan 29, 2022
RebeccaStevens added a commit to RebeccaStevens/type-fest that referenced this issue Jan 29, 2022
RebeccaStevens added a commit to RebeccaStevens/type-fest that referenced this issue Jan 29, 2022
RebeccaStevens added a commit to RebeccaStevens/type-fest that referenced this issue Jan 29, 2022
RebeccaStevens added a commit to RebeccaStevens/type-fest that referenced this issue Jan 31, 2022
RebeccaStevens added a commit to RebeccaStevens/type-fest that referenced this issue Jan 31, 2022
@skarab42
Copy link
Collaborator

Seems to be fixed with #359 (Playground)

@marekdedic
Copy link
Author

Thanks, trying it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants