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

Add generic Deep type #12

Open
krzkaczor opened this issue Feb 7, 2019 · 5 comments
Open

Add generic Deep type #12

krzkaczor opened this issue Feb 7, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@krzkaczor
Copy link
Collaborator

krzkaczor commented Feb 7, 2019

Is it possible to create generic Deep type that would minimize repetition between DeepPartial, DeepReadonly etc ?

@Andarist
Copy link
Contributor

Was thinking about it a little bit and I think this aint possible - although Im quite new to TS so I might miss something.

The challenge here is that each of those types does something different to property modifiers, and also we don't know how to map a property modifier to array modifier. So at the very least we would require 3 type parameters <RecursiveMapper, ObjectHandler, ArrayHandler> which seems more than convoluted and I still don't know how to apply ObjectHandler to the transformation.

@Beraliv
Copy link
Collaborator

Beraliv commented Aug 13, 2021

You want to have high order function in TypeScript (which is discussed here – microsoft/TypeScript#1213)

You still can define Replace and change one type with another:

declare const __placeholder__: unique symbol;

type Placeholder = { readonly [__placeholder__]: unknown };

type Replace<T, X, Y> = {
  [K in keyof T]: T[K] extends X ? Y : T[K];
};

But let me check that it can be implemented somehow

Also it has limitations with restrictions but we don't have them so might be possible

@Beraliv Beraliv added the enhancement New feature or request label Aug 15, 2021
@Beraliv
Copy link
Collaborator

Beraliv commented Aug 15, 2021

Replace has a lot of limitations, e.g.

  1. It isn't working if you don't know element types in advance – https://tsplay.dev/W4pbXW
  2. Can use Replace for one type of parameter, but if you have multiple placeholders, you need to have more and follow it really carefully
  3. You can Replace only on first level, or implement a deep replacement (might be difficult)

Also, you might need several implementation of Replace: for objects and primitives

@Beraliv
Copy link
Collaborator

Beraliv commented Feb 20, 2023

Several days ago I've seen work on hotscript - https://github.com/gvergnaud/hotscript

Maybe it will be possible to organise it so it would be possible to reuse the same types, although I didn't have a chance to play with it

@Beraliv
Copy link
Collaborator

Beraliv commented Apr 10, 2024

I've closed the PR because it is not a priority right now, I may come back to it at some point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants