Skip to content

Instantly share code, notes, and snippets.

@brunoh3art
Created May 18, 2024 21:53
Show Gist options
  • Select an option

  • Save brunoh3art/fd20f30bcf0f9523a92c8b7efa2efc57 to your computer and use it in GitHub Desktop.

Select an option

Save brunoh3art/fd20f30bcf0f9523a92c8b7efa2efc57 to your computer and use it in GitHub Desktop.
export type Replace<T, R> = Omit<T, keyof R> & R;
@brunoh3art
Copy link
Author

Example of Using the Replace Type

Example

Suppose we have two types: A and B. Type A has properties prop1, prop2, prop3, and prop4, while type B has properties prop3 and prop4. We want to create a new type X that replaces the properties of A with the properties of B.

type A = { prop1: string, prop2: string, prop3: string, prop4: string };
type B = { prop3: number, prop4: number };

type X = Replace<A, B>;

const x: X = { prop1: '1', prop2: '2', prop3: 3, prop4: 4 };

In this example, type X will have properties prop1, prop2, prop3, and prop4, with the correct values according to types A and B. You can adapt this pattern to your own needs by replacing types A and B with your own types and properties. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment