DEV Community

Discussion on: Advanced TypeScript Exercises - Question 5

Collapse
 
dwjohnston profile image
David Johnston

Here's a simpler solution than has been posted:

// Here declaration to be changed 🔥
declare function getUser<T extends Config>(
  config: T
): {
  name: T["name"] extends true ? string : never,
  lastname: T["lastname"] extends true ? string : never,
}; 
Enter fullscreen mode Exit fullscreen mode

Now in this case we're not getting compile errors - however the types of those values are never so depending on your use case - might be enough to prevent errors from occuring.

Collapse
 
macsikora profile image
Pragmatic Maciej

Hi David, again please pay attention I put 🛑 icon where the code should show compile error. Your implementation doesn't create such. So its not valid solution.