Skip to content

Instantly share code, notes, and snippets.

@graffhyrum
Created October 9, 2024 20:05
Show Gist options
  • Save graffhyrum/f20eab76ad9e97aa03eb098ea5492e15 to your computer and use it in GitHub Desktop.
Save graffhyrum/f20eab76ad9e97aa03eb098ea5492e15 to your computer and use it in GitHub Desktop.
Swaps the type of a parameter to something else
/**
 * Modify a type `T` by replacing properties with the properties of type `R`.
 *
 * @template T - The original type.
 * @template R - The type with properties to replace in `T`.
 */
type Modify<T, R> = Omit<T, keyof R> & R;
// Example usage of Modify type
interface Original {
  a: number;
  b: string;
}

interface Replacement {
  b: boolean;
  c: string;
}

type Modified = Modify<Original, Replacement>;
// Modified is now { a: number; b: boolean; c: string; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment