Decompose monolithic readme into docs/ directory structure
This commit is contained in:
27
docs/types/mapped.md
Normal file
27
docs/types/mapped.md
Normal file
@@ -0,0 +1,27 @@
|
||||
[Overview](../overview.md) | [Installation](../installation.md) | [Usage](../usage.md) | **Types** | [Values](../values/) | [Syntax](../syntax/) | [TypeRegistry](../type-registry.md) | [TypeCheck](../type-check.md)
|
||||
|
||||
# Mapped Types
|
||||
|
||||
TypeBox supports mapped types with the Mapped function. This function accepts two arguments, the first is a union type typically derived from KeyOf, the second is a mapping function that receives a mapping key `K` that can be used to index properties of a type. The following implements a mapped type that remaps each property to be `T | null`.
|
||||
|
||||
```typescript
|
||||
const T = Type.Object({
|
||||
// type T = {
|
||||
x: Type.Number(), // x: number,
|
||||
y: Type.String(), // y: string,
|
||||
z: Type.Boolean(), // z: boolean
|
||||
}); // }
|
||||
|
||||
const M = Type.Mapped(Type.KeyOf(T), (K) => {
|
||||
// type M = { [K in keyof T]: T[K] | null }
|
||||
return Type.Union([Type.Index(T, K), Type.Null()]); //
|
||||
}); // ... evaluated as
|
||||
//
|
||||
// const M: TObject<{
|
||||
// x: TUnion<[TNumber, TNull]>,
|
||||
// y: TUnion<[TString, TNull]>,
|
||||
// z: TUnion<[TBoolean, TNull]>
|
||||
// }>
|
||||
```
|
||||
|
||||
Back to [Home](../../readme.md)
|
||||
Reference in New Issue
Block a user