/** * Creates a new type by taking an existing type T, * and making a specified subset of its properties (K) required. * Properties in K must exist in T and be optional initially. */ type WithRequired = T & { [P in K]-?: T[P] }; interface UserProfile { id: string; name: string; email?: string; phone?: string; address?: { street: string; city: string; }; } // Example 1: Make 'email' required type UserProfileWithRequiredEmail = WithRequired; const user1: UserProfileWithRequiredEmail = { id: 'u1', name: 'Alice', email: 'alice@example.com', // Must be provided }; // const user2: UserProfileWithRequiredEmail = { // Type Error: Property 'email' is missing // id: 'u2', // name: 'Bob', // }; // Example 2: Make 'email' and 'phone' required type UserProfileWithRequiredContact = WithRequired; const user3: UserProfileWithRequiredContact = { id: 'u3', name: 'Charlie', email: 'charlie@example.com', phone: '123-456-7890', }; // const user4: UserProfileWithRequiredContact = { // Type Error: Property 'phone' is missing // id: 'u4', // name: 'David', // email: 'david@example.com', // }; console.log('--- WithRequired Demo ---'); console.log(user1); console.log(user3);