Suppose I have a class, say, BasicModal and one type describing BasicModal's configurations, type BasicModalConfig = {} for instance, and I also have a function, that takes the class and its settings a parameters, let's assume its signature is function openModal(component, configs);. The goal is to infer the component's type (or class name if useful/possible) and use it in config's type as a type parameter and set the configurations value accordingly, in other words, mapped types, but without the nested ternary madness.
Example of the desired result:
// This works
interface BasicModalConfig {
autoClose: boolean;
};
class BasicModal {}
// This would get out of hand very fast
type TypeResolver<K> = K extends BasicModal ? BasicModalConfig : never;
function openModal<T>(component: T, configs: TypeResolver<T>) {}
openModal(BasicModal, {
autoClose: true // this should give me hints/autocompletion
});
// This doesn't work
interface Resolver {
BasicModal: BasicModalConfig;
}
//or (doesn't compile)
//type Resolver = {
// [x: typeof BasicModal]: BasicModalConfig;
//};
function openModal2<T>(component: T, configs: Resolver[T extends keyof Resolver ? T : never]
) {}
openModal2(BasicModal, {
autoClose: true // this should give me hints/autocompletion, but config is "never"
});
I searched for something like "nameof" from C#, in Typescript repository, to use it as a key value, but it's still in discussion: https://github.com/microsoft/TypeScript/issues/1579
As of typescript version 4.6, is it possible to even achieve this?
Here's a Typescript playground.