Readonly
#
introductionImplement the built-in Readonly<T>
generic without using it.
Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned.
For example
ts
interface Todo {title: string;description: string;}type MyReadonly<T> = any;const todo: MyReadonly<Todo> = {title: 'Hey',description: 'foobar',};todo.title = 'Hello'; // Error: cannot reassign a readonly propertytodo.description = 'barFoo'; // Error: cannot reassign a readonly property
View on GitHubts
interface Todo {title: string;description: string;}type MyReadonly<T> = any;const todo: MyReadonly<Todo> = {title: 'Hey',description: 'foobar',};todo.title = 'Hello'; // Error: cannot reassign a readonly propertytodo.description = 'barFoo'; // Error: cannot reassign a readonly property
#
start pointtsTry
/* _____________ Your Code Here _____________ */typeMyReadonly <T > = any;Â/* _____________ Test Cases _____________ */typeType 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.cases = [Expect <Equal <MyReadonly <Todo1 >,Readonly <Todo1 >>>];ÂinterfaceTodo1 {title : string;description : string;completed : boolean;meta : {author : string;};}
take the challengetsTry
/* _____________ Your Code Here _____________ */typeMyReadonly <T > = any;Â/* _____________ Test Cases _____________ */typeType 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.cases = [Expect <Equal <MyReadonly <Todo1 >,Readonly <Todo1 >>>];ÂinterfaceTodo1 {title : string;description : string;completed : boolean;meta : {author : string;};}