Example:
// ===== Declaration ===== //
class A {
CONSTS_TYPE: { [key: string]: [any] }
CONSTS: { [key in keyof this['CONSTS_TYPE']]: key }
foo<T extends keyof this['CONSTS_TYPE'] | string>(
type: T,
callback: (...args: T extends keyof this['CONSTS_TYPE'] ? this['CONSTS_TYPE'][T] : any) => any
) { /** some implementation*/ }
}
class B extends A {
CONSTS_TYPE: {
aa: [number],
bb: [string]
}
// Here is the problem
// Type '{ aa: "aa"; bb: "bb"; }' is not assignable to type '{ [key in keyof this["CONSTS_TYPE"]]: key; }'.(2322)
CONSTS: { [key in keyof this['CONSTS_TYPE']]: key } = {
aa: 'aa',
bb: 'bb'
}
}
// ===== Call ===== //
const b = new B;
b.foo(b.CONSTS.aa, (arg) => {
// so that i can know 'arg' is a 'number' type
arg // number type
});
it works well, but not too well.
i know '// @ts-ignore' will work really well
but I think there may be other solutions