Go to repository

Type Alias WithRequired<T, K>

WithRequired<T, K>: T & {
    [P in K]-?: T[P]
}

A type modifier that allows to construct a generic type that requires only one property of a given type.

Type Parameters

  • T

    The base type.

  • K extends keyof T

    the name of the property to require in the new type.

Conider that we have a type

type User = {
id: string
name?: string
email?: string
}

Then we can create a type like so:

type UserWithName = WithRequired<User, 'name'>