-
-
Notifications
You must be signed in to change notification settings - Fork 349
Is it possible to make Provider and @inject type safe? #143
Comments
If export type stores = 'store1' | 'store2';
interface MyStoresProviderInterface { new (): Provider<stores> } ;
const MyStoresProvide = Provider as MyStoresProviderInterface;
const provided = <MyStoresProvide store1={store1} store1={store2} /> Then later when injecting import stores from './app.ts' // 'store1' | 'store2
typedInject = (store: stores) => inject.bind(inject, store)
@typedInject('store1'); // this will make sure string 'store1' is type safe I don't know TypeScript well enough to say this is a proper way of making Provider and |
@mohsen1, I don't think your example will work (especially as strong typing Provider is actually not very interesting, it is on inject where you want to get the proper types). I think the closest way to get there is described here: https://github.com/mobxjs/mobx-react#strongly-typing-inject. If you feel it can be better typed, feel free to submit a PR! |
I liked the example in README but it's not as type safe as it should be. When you use If class MobXReact<StoreType[]> {
public Provider(...stores: StoreType[]) {
return Provider.bind(Provider, ...stores);
}
public inject(...stores: StoreType[]) {
return inject.bind(inject, ...stores)
}
} But those are two independent functions that can't really share generic types without a bridge. |
Yep, and there might be many different configs of Provider and inject in the same app. Unless somebody has a genius idea, I do't think this is feasible |
Currently
Provider
and@inject
are using strings for providing stores and injecting them in components. Is it possible to use TypeScript generics to make sure that those strings are type checked by TypeScript compiler? Since strings can be their own types in TypeScript it should be possible to take advantage of that feature.The text was updated successfully, but these errors were encountered: