

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Conditional Types
incomplete
2: Infer
incomplete
3: Mapped Types
incomplete
4: Mapped Types With Conditionals
incomplete
5: Extracting Keys from Types
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
So if mapped types are a step down the road to type tom-foolery, conditional mapped types are one more leap. I'm not saying they're not cool, or that they're not useful (they are in certain scenarios), but you can create some really hard to read code if you're not careful. So use them wisely.
Let's take our OptionalSoldier example from before:
type Soldier = {
name: string;
age: number;
branch: "garrison" | "military police" | "survey corps";
};
type OptionalSoldier = {
[K in keyof Soldier]?: Soldier[K];
};
What if instead of making all the properties optional, we instead wanted to filter any non-string properties? We can do that with a conditional mapped type:
type FilteredSoldier = {
[K in keyof Soldier]: Soldier[K] extends string ? Soldier[K] : never;
};
The conditional: Soldier[K] extends string only evaluates to true (and thus the property is included as Soldier[K]) if the property is assignable to string. Otherwise, it evaluates to never, and the property is excluded. One really cool thing to note, is that because we used Soldier[K] in the conditional, the more specific type of the branch property is preserved, resulting in a type of:
type FilteredSoldier = {
name: string;
// age: never;
branch: "garrison" | "military police" | "survey corps";
};
The compliance team at Support.ai is reviewing what user-visible data can be edited, and what should be locked.
Use T[K] extends Function | object to check each field.