TypeScript - RequireOnly

less than 1 minute read

Very useful utility type definition when we need to define a type having partial mandatory properties in a type.

1
type RequireOnly<T, P extends key of T> = Pick<T, P> & Partial<Omit<T, P>>
  • Example
1
2
3
4
5
6
7
type Task = {
    id: string;
    title: string;
    desc: string;
}

type DraftTask = RequireOnly<T, 'title' | 'id'>

Categories:

Updated: