Skip to content

Instantly share code, notes, and snippets.

@SimGus
SimGus / unsubscribe.ts
Last active Jan 13, 2020
Observable unsubscribing inside Angular components
View unsubscribe.ts
////////////////////////////////////////////////////////////////////////////////////////////
// In Angular, we often use *RxJS* observables. //
// //
// A frequent usecase of observables within Angular component is to have a subscription //
// to an observable for the whole lifetime of that component. //
// //
// To do this, we need to subscribe when the component comes up, //
// and we will *HAVE to* unsubscribe when the component is destroyed. //
// (Seriously, not unsubscribing will lead to memory leaking everytime the component gets //
// destroyed.) //
@SimGus
SimGus / maps.ts
Created Jan 8, 2020
Maps (dicts) in TypeScript
View maps.ts
// When programming, there will certainly be times you will want to associate values to other values,
// that is building a dictionary.
// In TypeScript, a `Map<K, V>` exists (it seems it actually is the one defined in JavaScript ES6).
// However, at least in TypeScript 3.1.6, objects from this class are not very reliable:
// You will sometimes get errors such as "TypeError: map.get is not a function", at seemingly random times.
// A solution is to replace `Map<string, V>` by plain objects defined as `{ [key: string]: V }`.
// Another solution seems to be to replace them by `Record<string, V>`.