Search code examples
ngxs

Strongly typed selectors


I am new to NGXS and can't find an answer to my question.

Is it possible to strongly type Selectors in NGXS so that intellisense works in Visual Studio Code and and errors can be caught at compile time?


Solution

  • Yep, you need to use something like a state token.

    Here's an example

    The basics are that you need to use something in your selector that is typed, a string is not typed, ie:

    @Select(TODOS_STATE_TOKEN) good$: Observable<TodosStateModel>;
    
    // Uncomment the next line, it won't compile
    // @Select(TODOS_STATE_TOKEN) bad$: Observable<string[]>;
    
    // this is typed as any still
    @Select(TODOS_STATE_TOKEN) any$;