Sobes.tech
Middle

Live coding: implement your own type MyOmit<T, K> (similar to the Omit utility in TypeScript)

sobes.tech AI

Answer from AI

The type MyOmit<T, K> should create a new type based on T, excluding the keys K.

Implementation using mapped types and conditional types:

// K extends keyof T — keys to exclude
// [P in keyof T as P extends K ? never : P]: T[P] — iterate over keys of T, excluding K

type MyOmit<T, K extends keyof T> = {
  [P in keyof T as P extends K ? never : P]: T[P]
};

// Example usage:
interface User {
  id: number;
  name: string;
  age: number;
}

type UserWithoutAge = MyOmit<User, 'age'>;
// Result: { id: number; name: string; }
Live coding: implement your own type MyOmit<T, K>… - sobes.tech