Readonly - TypeScript Challenge Explained
Mar 12, 2023
My solution to the Readonly TypeScript Challenge as part of my journey to Understanding the TypeScript Challenges.
Given a type T, the utility should set the readonly keyword for all properties.
Solution
type MyReadonly<T> = {
readonly [K in keyof T]: T[K]
}
Explanation
{ }return an object/record typereadonlyset the "readonly" keyword for each item[K in keyof T]loop over the keysT[K]Select the value from the original typeTfor the each keyK