-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Added in some of the great info about non-homomorphic transformations… #471
Conversation
@sandersn can you take a look |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the discussion should be broken up and moved toward the end of the mapped type section since it's an advanced concept.
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
a type `EnsureAll` | ||
|
||
```ts | ||
type EnsureAll<T, K extends string> = { |
There was a problem hiding this comment.
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.
``` | ||
|
||
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. |
There was a problem hiding this comment.
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.
I think my requests amount to rewriting the whole PR, so I made a PR at #473 that does more or less that. @JakeGinnivan @mhegazy do you want to take a look? I like the |
Sounds good to me @sandersn, was just an area where I had some difficulty making it work the way I wanted. |
a type `EnsureAll` | ||
|
||
```ts | ||
type EnsureAll<T, K extends string> = { |
There was a problem hiding this comment.
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
.
@sandersn Could you please add an example for I'm testing the following with 2.2.2, and can't get it to work: type Required<T, K extends keyof T> = {
[P in K]: T[P]
};
interface X {
y?: string
}
let safeX: Required<X, keyof X> = {}; // Assignment should have failed
let y: string = safeX.y; // Error: string | undefined is not assignable to string |
… in type mapping from microsoft/TypeScript#13224 (comment)
Just trying to extend the docs for something I could not figure out how to do after reading the docs on type mapping. // @mhegazy