Skip to content
This repository has been 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

Conversation

JakeGinnivan
Copy link
Contributor

… 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

@mhegazy
Copy link
Contributor

mhegazy commented Jan 3, 2017

@sandersn can you take a look

Copy link
Member

@sandersn sandersn left a 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
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.

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.

```

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.

@sandersn
Copy link
Member

sandersn commented Jan 3, 2017

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 EnsureAll example but I ended up using Pick and Record as examples since they were already discussed as part of the built-in types.

@JakeGinnivan
Copy link
Contributor Author

Sounds good to me @sandersn, was just an area where I had some difficulty making it work the way I wanted.

@JakeGinnivan JakeGinnivan deleted the reverse-partial-docs branch January 4, 2017 01:21
a type `EnsureAll`

```ts
type EnsureAll<T, K extends string> = {
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.

@masaeedu
Copy link

masaeedu commented Apr 5, 2017

@sandersn Could you please add an example for EnsureAll as well? It isn't clear how to do the reverse transformation of Partial, and I suspect it might not be possible in the current type system.

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

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.

4 participants