Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Details About: ReadonlySet?

Is ReadonlySet means set of ReadOnly properties as same as ReadOnlyCollection in C#? I am not able to find out any document for it from anywhere. Could you please let me know what is the use of ReadonlySet and How can we do implementation of it?

let readonly: Readonly<number> = 1;
let readonlySet: ReadonlySet<number> = ???// 
like image 830
Ramesh Rajendran Avatar asked Jan 18 '18 15:01

Ramesh Rajendran


1 Answers

ReadonlySet is a type similar to Set, but it does not have a add / delete method, so elements can only be added during construction:

  const regular: Set<number> = new Set([1,2,3]);
  regular.add(4);
  
  const readonly: ReadonlySet<number> = new Set([1,2,3]);
  readonly.add(4) //fails, add not a function

Reference

Unlike the readonlySet in C# you referenced, typescripts ReadonlySet is not actually a class/constructor. Its just a type. As typescript has duck typing, the Set is kind of a superclass to ReadonlySet, so the type can be "typecasted". This is actually only for typechecking, it has no runtime influence.At runtime, it's just a JavaScript Set.

like image 133
Jonas Wilms Avatar answered Oct 11 '22 08:10

Jonas Wilms