Reputation: 1300
I want to create a dictionary with Types as values.
f.e, in C#, i'd do: Dictionary<Type, WhateverINeed>
, but in typescript there's really I know that can do that.
I've come across type.d.ts
in @angulalr/core
, but I don't know how to use it. I've seen the generic type class used the Route
interface, where you provide the Component class for the route.
Basically, I want to create a map between a type and an Observable
of that class
But the only dictionary key types I know can be used with Typescript's dictionary are int
and string
.
The usecase would be either:
service.method<MyClass>(...)
or
service.method(MyClass, ...)
Thanks for helping!
Upvotes: 1
Views: 5826
Reputation: 15288
This is basically type safe wrapper for Map<,>
and it will only work with concrete classes (functions):
import {Type} from '@angular/core';
class Service
{
private map = new Map<Type<any>, Observable<any>>();
public set<T>(key:Type<T>, value:Observable<T>):void
{
this.map.set(key, value);
}
public get<T>(key:Type<T>):Observable<T>
{
return this.map.get(key);
}
}
const service = new Service();
service.set(MenuModel, Observable.of(new MenuModel()));
service.set(Number, Observable.of(0));
service.get(MenuModel).subscribe(model => console.debug('menu', model.userMenu));
service.get(Number).subscribe(model => console.debug('number', model));
Upvotes: 4