diff --git a/peps/pep-0827.rst b/peps/pep-0827.rst index f09be37b1f1..3f596f0a8ce 100644 --- a/peps/pep-0827.rst +++ b/peps/pep-0827.rst @@ -1193,6 +1193,57 @@ until one of them is empty. ) +.. _pep827-ts-utils: + +TypeScript-style "Utility Types" +-------------------------------- + +TypeScript defines a number of `utility types +`__ +for performing common type operations. + +We present implementations of a selection of them:: + + # Pick + # 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 + # 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 + # 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 + # 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 =========