For instance:
// ===== Declaration ===== //
class A {
CONSTANTS_TYPE: { [key: string]: [any] }
CONSTANTS: { [key in keyof this['CONSTANTS_TYPE']]: key }
bar<T extends keyof this['CONSTANTS_TYPE'] | string>(
type: T,
callback: (...args: T extends keyof this['CONSTANTS_TYPE'] ? this['CONSTANTS_TYPE'][T] : any) => any
) { /** some implementation*/ }
}
class B extends A {
CONSTANTS_TYPE: {
aa: [number],
bb: [string]
}
// Issue Here
// Type '{ aa: "aa"; bb: "bb"; }' is not assignable to type '{ [key in keyof this["CONSTANTS_TYPE"]]: key; }'.(2322)
CONSTANTS: { [key in keyof this['CONSTANTS_TYPE']]: key } = {
aa: 'aa',
bb: 'bb'
}
}
// ===== Call ===== //
const b = new B;
b.bar(b.CONSTANTS.aa, (arg) => {
// so I can determine if 'arg' is of type 'number'
arg // number type
});
it's functional, but not ideal.
I am aware that '// @ts-ignore' would provide a quick fix
however, I believe there might be alternative solutions