Iterator Pattern With Custom Collection

Implement a typed custom iterator for a collection with filtering and mapping lazy evaluation.

TypescriptData Fetching

Code

snippet
export class CustomCollection<T>implements Iterable<T>{constructor(private items:T[]){}[Symbol.iterator]():Iterator<T>{let i=0;const items=this.items;return{next():IteratorResult<T>{return i<items.length?{done:false,value:items[i++]}:{done:true,value:undefined as unknown as T};}};}filter(pred:(v:T)=>boolean):CustomCollection<T>{return new CustomCollection(this.items.filter(pred));}map<U>(fn:(v:T)=>U):CustomCollection<U>{return new CustomCollection(this.items.map(fn));}toArray():T[]{return[...this.items];}}