The instructions for this challenge are:
Implement the built-in
Readonly<T>
generic without using it.Constructs a type with all properties of
T
set toreadonly
, meaning the properties of the constructed type cannot be reassigned.
Example code that should compile correctly is provided:
interface Todo {
title: string
description: string
}
const todo: MyReadonly<Todo> = {
title: "Hey",
description: "foobar"
}
The initial type is type MyReadonly<T> = any
.
We’re back in the realm of the easy challenges. This one is particularly easy thanks to the built-in typescript
mapping modifiers. We should
follow
the pattern of the earlier challenges where we used a mapped type but apply the mapping modifier to make it readonly
.
We can do this by adding +readonly
in front of the mapped type statement. The rest of the statement is
straightforward.
This results in an eventual solution of:
type MyReadonly<T> = { +readonly [P in keyof T]: T[P] }