Skip to content

RFC: Improvement of Typings for NGXS State Operators #1805

Discussion options

You must be logged in to vote

Behold! One little trick, and everything works as expected.

    // Behold! The little `extends infer S ? S : never` trick allows Typescript to infer the intended shape of T from
    // the usage of customOperator within a surrounding function.
    // I forgot why this works, but NgRx uses this trick for their `on` function in their reducers.
    function customOperator<T>(patchObject: PatchSpec<T extends infer S ? S : never>): StateOperator<T> {
        const patchValue: Partial<T> = patchObject as any; // Implementation not included
        return (state: T) => ({ ...state, ...patchValue });
    }

Replies: 3 comments 13 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
13 replies
@david-shortman
Comment options

@david-shortman
Comment options

@david-shortman
Comment options

@timdeschryver
Comment options

@david-shortman
Comment options

Answer selected by markwhitfeld
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants