Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions peps/pep-0827.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,57 @@ until one of them is empty.
)


.. _pep827-ts-utils:

TypeScript-style "Utility Types"
--------------------------------

TypeScript defines a number of `utility types
<https://www.typescriptlang.org/docs/handbook/utility-types.html>`__
for performing common type operations.

We present implementations of a selection of them::

# Pick<T, Keys>
# Constructs a type by picking the set of properties Keys from T.
type Pick[T, Keys] = typing.NewProtocol[
*[
p
for p in typing.Iter[typing.Members[T]]
if typing.IsAssignable[p.name, Keys]
]
]

# Omit<T, Keys>
# Constructs a type by picking all properties from T and then removing Keys.
type Omit[T, Keys] = typing.NewProtocol[
*[
p
for p in typing.Iter[typing.Members[T]]
if not typing.IsAssignable[p.name, Keys]
]
]

# Partial<T>
# Constructs a type with all properties of T set to optional (T | None).
type Partial[T] = typing.NewProtocol[
*[
typing.Member[p.name, p.type | None, p.quals]
for p in typing.Iter[typing.Attrs[T]]
]
]

# PartialTD<T>
# Like Partial, but for TypedDicts: wraps all fields in NotRequired
# rather than making them T | None.
type PartialTD[T] = typing.NewProtocol[
*[
typing.Member[p.name, NotRequired[p.type], p.quals]
for p in typing.Iter[typing.Attrs[T]]
]
]


Rationale
=========

Expand Down
Loading