I am designing a settings store system where users can add settings with values of any type and retrieve them with correct typing. To achieve this, I thought about utilizing connected generic type arguments. The idea is that the return type of the value should be inferred based on the type argument passed to the `getSetting()` method. By using the type as a key, the system should be able to fetch the correct information.
Currently, I have implemented a method `getSetting()` that takes a type as input and should infer the return type accordingly. However, I have encountered a problem where the return type of `getSetting` is always shown as `unknown`, even though I expected it to be `number` in a specific example.
abstract class Setting<T> {
abstract value: T;
}
class SD extends Setting<number> {
value = 0;
}
let getSetting = <T extends Setting<K>, K>(type: new () => T): K => {
// return value somehow
};
let setting = getSetting(SD); // the type of setting is shown as unknown instead of number
I am seeking to understand where the error lies in my approach, or if there might be a better way to achieve the desired functionality. Could there be an alternative solution to this problem?