Type infer in Environment injected service

Discussão em 'Angular' iniciado por Pizzicato, Janeiro 9, 2025.

  1. Pizzicato

    Pizzicato Guest

    In Angular I created a function, say provideSomething, that can be used in the application bootstrap process to be able to inject a service, MyService. This function receives an object as a parameter, for simplicity let's say it has a Record<string, number> type:

    export function provideSomething<T extends Record<string, number>>(config: T): EnvironmentProviders {
    return makeEnvironmentProviders([
    {
    provide: MyService,
    useFactory: (authStore: AuthStore) => new MyClass(config)
    },
    ]);
    }


    MyClass:

    export class MyClass<T extends Record<string, number>> {
    constructor(private readonly config: T) {}

    get config(): T {
    return this.config;
    }
    }


    All this is such a simplification of what I'm doing that doesn't make much sense, but it gets to my point: I'd like to be able to have automatic type inference in the object returned by the config getter, based on the parameter received by provideSomething, in the example's case: { prop1: number, prop2: number}

    Is there any way this can be achieved?

    Continue reading...

Compartilhe esta Página