Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Added in some of the great info about non-homomorphic transformations… #471

Closed
Closed
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
23 changes: 23 additions & 0 deletions pages/Advanced Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,29 @@ type PersonPartial = Partial<Person>;
type ReadonlyPerson = Readonly<Person>;
```

Property mapping is a [homomorphic](https://en.wikipedia.org/wiki/Homomorphism) transformation, so if you wanted to do the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too early for this explanation, and it's too dense for the handbook. It would be better to weave it in with the "Real applications" discussion below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Homomorphic mapped types are already described briefly in the "Inference from mapped types" section, so that section needs to be updated as well.

opposite of the `Partial` type and make all optional properties required (a non-homomorphic transformation) you could create
a type `EnsureAll`

```ts
type EnsureAll<T, K extends string> = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this example but its description of purpose is confusing.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you actually test this EnsureAll type? I get a type error saying you can't index T with K, presumably because K isn't constrained to the keys of T.

[P in K]: T[P];
}
```

And to use it:

```ts
interface Person {
name: string;
age?: number;
}
type PersonWithAge = EnsureAll<Person, keyof Person> // { name: string, age: number }
```

Notice the constraint on K is string; this way the type system can not assert that the result of this
transformation is a homomorphic mapping on T, and thus no modifiers are copied through.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need an example of coping vs not-copying.


Let's take a look at the simplest mapped type and its parts:

```ts
Expand Down