type-challenges/questions/2759-medium-requiredbykeys
2021-08-19 12:51:50 +00:00
..
2021-08-19 20:51:22 +08:00
2021-08-19 12:51:50 +00:00
2021-08-19 20:51:22 +08:00
2021-08-19 20:51:22 +08:00

RequiredByKeys medium #object

by jiangshan @jiangshanmeta

Take the Challenge

Implement a generic RequiredByKeys<T, K> which takes two type argument T and K.

K specify the set of properties of T that should set to be required. When K is not provided, it should make all properties required just like the normal Required<T>.

For example

interface User {
  name?: string
  age?: number
  address?: string
}

type UserPartialName = RequiredByKeys<User, 'name'> // { name: string; age?: number; address?: string }


Back Share your Solutions Check out Solutions