3

New to typescript and struggling to describe a dictionary.

Basically I export several stores (the example below is reduced on purpose), which I feed to a Registry class.

Ideally the stores dictionary type I would like to be dynamic in a way that I can export as many stores I want, but when I try to use the registry in my component and try to access something that is not in the stores dict, TS will complain and the IDE will pick it up.

Any TS wizardry will be much appreciated

stores.ts

import UserStore from './user.ts';
import ListStore from './list.ts';
export {
   UserStore, ListStore
};

registry.ts


export default class Registry{

    stores = {};

    constructor(stores: any){
        Object.keys(stores).forEach((name: string) => {
            const cls = stores[name];
            this.stores[name] = new cls(this);
        });
    }

UPDATE based on the answer provided below. I changed the registry to this. All stores derive from BaseStore. However, I don't get TS complaining when I try to access a non existent store

export default class Registry{

    stores: { [name: string]: BaseStore } = {};


    constructor(stores: any){
        Object.keys(stores).forEach((name: string) => {
            const cls = stores[name];
            if(cls){
                this.stores[name] = new cls(this);
            }
        });
3
  • What makes a store a store? Store subclasses? A link to a minimal, reproducible example on TS Playground would be helpful. Commented Nov 25, 2021 at 19:41
  • @fedonev A "store" is a concept in Redux, Flux, and ReactJS. It's not possible to make a TS Playground example because you need the rest of ReactJS available. Commented Nov 25, 2021 at 21:19
  • 1
    How would you call/instantiate the Registry class? Could you provide an example, please? Commented Nov 25, 2021 at 23:01

1 Answer 1

2
type storeType = {
    [key: string]: any // instead of any you can put any type you want...
}

now stores can have this:

const stores = {
    'userStore': 'anything',
    'listStore': 'anything'
}

console.log(stores.userStore) // will accept the type
Sign up to request clarification or add additional context in comments.

4 Comments

I'd upvote you, but this is not a suitable use of any in TypeScript today.
I think this is more a way to bypass the TS checker and not really the way we should want to implement such things
@Yitzhak I tried that already. However, this way I do not get the IDE complaining if I try to reference a non existent store
yea well that's because you are asking that stores will be dynamic, you can have it be dynamic and complain if the store does not exist at the same time, you need to decide if you want to add each time new store to the type store (for example) or just do a dynamic type as I wrote above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.