Advanced TypeScript Exercises - Question 5

Pragmatic Maciej - Feb 28 '20 - - Dev Community

We have function getUser which gets Config object, the object defines what fields of User function will return. If for example config says { name: true, lastname: false } it means returned object should have name field non-optional but no field lastname. Current User type is very broad type of the return, we need to narrow it down depending on the config passed as argument of getUser. Solution should be done only at the type level, no value level code should be written. Only function declaration getUser is to be changed.

// Here types should remain the same ❄
type Config = {
  name: boolean;
  lastname: boolean;
};
type User = {
  name?: string;
  lastname?: string;
};

// Here declaration to be changed πŸ”₯
declare function getUser(
     config: Config
): User;

// test cases
const user = getUser({ name: true, lastname: false })
user.name // this field should be non-optional
user.lastname // this field should not be there and we should have compile error πŸ›‘

const user2 = getUser({ name: true, lastname: true })
user2.name // this field should be non-optional
user2.lastname // this field should be non-optional

const user3 = getUser({ name: false, lastname: true })
user3.name // this field should not be there and we should have compile error πŸ›‘
user3.lastname // this field should be non-optional

const user4 = getUser({ name: false, lastname: false })
user4 // user4 should be empty object {}
Enter fullscreen mode Exit fullscreen mode

Full code can be found in the playground.

Post your answers in comments. Have fun! Answer will be published soon!

This series is just starting. If you want to know about new exciting questions from advanced TypeScript please follow me on dev.to and twitter.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player