BFE.dev solution for TypeScript puzzle
2. implement Required<T>
Looking at the code example, we can see that the requirment is to remove the ? on each property name.
To achieve this, we need to use the -
, introduced in TypeScript 2.8.
-
allows us to remove modifiers like readonly
and ?
.
type MutableRequired<T> = { -readonly [P in keyof T]-?: T[P] }; // Remove readonly and ?
type ReadonlyPartial<T> = { +readonly [P in keyof T]+?: T[P] }; // Add readonly and ?
So the answer to this question is simply a Mapped Type with -
.
type MyRequired<T> = {
[P in keyof T]-?: T[P]
}
You might also be able to find a solution fromcommunity posts or fromAI solution.