File tree 1 file changed +23
-0
lines changed
1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -793,6 +793,29 @@ type PersonPartial = Partial<Person>;
793
793
type ReadonlyPerson = Readonly <Person >;
794
794
```
795
795
796
+ Property mapping is a [ homomorphic] ( https://en.wikipedia.org/wiki/Homomorphism ) transformation, so if you wanted to do the
797
+ opposite of the ` Partial ` type and make all optional properties required (a non-homomorphic transformation) you could create
798
+ a type ` EnsureAll `
799
+
800
+ ``` ts
801
+ type EnsureAll <T , K extends string > = {
802
+ [P in K ]: T [P ];
803
+ }
804
+ ` ` `
805
+
806
+ And to use it:
807
+
808
+ ` ` ` ts
809
+ interface Person {
810
+ name: string ;
811
+ age? : number ;
812
+ }
813
+ type PersonWithAge = EnsureAll <Person , keyof Person > // { name: string, age: number }
814
+ ` ` `
815
+
816
+ Notice the constraint on K is string; this way the type system can not assert that the result of this
817
+ transformation is a homomorphic mapping on T, and thus no modifiers are copied through.
818
+
796
819
Let's take a look at the simplest mapped type and its parts:
797
820
798
821
` ` ` ts
You can’t perform that action at this time.
0 commit comments