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 a type "Swap<T, Replaced, Replacing> #176

Open
euberdeveloper opened this issue Mar 6, 2022 · 1 comment
Open

Add a type "Swap<T, Replaced, Replacing> #176

euberdeveloper opened this issue Mar 6, 2022 · 1 comment

Comments

@euberdeveloper
Copy link

Is your feature request related to a real problem or use-case?

If an API returns an object with some dates, such as

interface Resource {
     id: string;
    value: number;
    created: Date;
}

The date will become a string due to json conversion:

interface ResourceReturned {
     id: string;
    value: number;
    created: string;
}

I want a type such that:

type ResourceReturned = Swap<Resource, Date, string>;

Describe a solution including usage in code example

A solution could be

export type Swap<Interface, ReplacedType, ReplacingType> = {
    [k in keyof Interface]: Interface[k] extends ReplacedType ? ReplacingType : Interface[k];
};

But note that it does not support deep, so it should be perfectionated.

Who does this impact? Who is this for?

Everyone that has my problem

Describe alternatives you've considered (optional)

Additional context (optional)

@ashatyk
Copy link

ashatyk commented Oct 20, 2022

I worked on such problem and my idea is:

type _Update<T, U, S> = {
    0: Exclude<U, T> extends T ? (Exclude<T, U> | S) : T,
    1: S
}[[T] extends [U] ? [U] extends [T] ? 1 : 0 : 0]

type Update<T extends object, U, S> = {
    [K in keyof T]: T[K] extends object ? Update<T[K], U, S> : _Update<T[K], U, S>
}

type a1 = {
    b1: symbol | string | number
    b2: symbol | string
    b3: symbol
    b4: string
    b5: {
        c1: 'c1'
        c2: number
        c3: number | symbol
    }
}

type test = Update<a1,symbol, boolean>

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

No branches or pull requests

2 participants