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

More discussion of homomorphic mapped types - #473

Merged
Nathan Shively-Sanders (sandersn) merged 4 commits into
masterfrom
homomorphic-mapped-types
Jan 4, 2017
Merged

Nathan Shively-Sanders (sandersn) merged 4 commits into
masterfrom
homomorphic-mapped-types

Conversation

@sandersn

Copy link
Copy Markdown
Member

Especially the practical uses and comparison to non-homomorphic types.

Especially the practical uses and comparison to non-homomorphic types.
Comment thread pages/Advanced Types.md Outdated

In these examples, the properties list is `keyof T` and the resulting type is some variant of `T[P]`.
This is a good template for any general use of mapped types.
That's because this kind of transformation is homomorphic, which means that the mapping applies to every property of `T` and no others.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

might be useful to add a link to wikipedia's definition of Homomorphism: https://en.wikipedia.org/wiki/Homomorphism

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Comment thread pages/Advanced Types.md Outdated
}
```

`Readonly` and `Partial` are homomorphic whereas `Pick` and `Record` are not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

that is not accurate, Pick is homorophic, since it is declared as type Pick<T, K extends keyof T>, the constraint makes is a homorphic relationship. this is a change in TS 2.1.5, that is not in TS 2.1.4. see microsoft/TypeScript#12826

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To be clear a homomorphic type is one of the form:

type H1<T> = { [P in keyof T] ... };
type H2<T, K extends keyof T> = { [P in K] ... };

basically any way for the type system to know that the mapped type is a transfomration on another type.
H3 however, is not a homomorphic mapping:

type H3<T, K extends string> = { [P in K]: Op<T> };

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nice catch. I forgot about that change. Updated.

Comment thread pages/Advanced Types.md Outdated
Homomorphic mapped types are mapped types that iterate over every property of some type, and only those properties: `{ [P in keyof T]: X }`.
In the examples above, `Nullable` and `Partial` are homomorphic whereas `Pick` and `Record` are not.
One clue is that `Pick` and `Record` both take a union of property names in addition to a source type, which they use instead of `keyof T`.
Note that this unwrapping inference works best on homomorphic mapped types.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • works only ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll fix the discussion.

Comment thread pages/Advanced Types.md Outdated
One clue is that `Pick` and `Record` both take a union of property names in addition to a source type, which they use instead of `keyof T`.
If the mapped type is not homomorphic you might have to explicitly give a type parameter to your unwrapping function.
Note that this unwrapping inference only works on homomorphic mapped types.
If the mapped type is not homomorphic you will have to explicitly give a type parameter to your unwrapping function.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

give an explicit type parameter.

@DanielRosenwasser

Copy link
Copy Markdown
Member

👍

@sandersn
Nathan Shively-Sanders (sandersn) deleted the homomorphic-mapped-types branch January 4, 2017 16:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants