I am puzzled by the fact that doSomething1()
and doSomething2()
are not being treated equally.
class Example<
TypeA = void,
TypeB = void,
ArgsType = { valueA?:TypeA, valueB?:TypeB }
> {
valueA?:TypeA;
valueB?:TypeB;
constructor( valueA:TypeA, valueB:TypeB ) {
this.valueA = valueA;
this.valueB = valueB;
}
doSomething1( args: { valueA?:TypeA, valueB?:TypeB } ) {
// this works
const { valueA, valueB } = args;
console.log( valueA );
console.log( valueB );
}
doSomething2( args: ArgsType ) {
// this throws "Property 'valueA' does not exist on type 'unknown'"
const { valueA, valueB } = args;
console.log( valueA );
console.log( valueB );
}
}
When I define all parameters manually as in doSomething1()
, everything functions as expected. However, when I attempt to consolidate them next to where TypeA
and TypeB
are defined using a new generic type for arguments, I encounter an error stating
Property 'valueA' does not exist on type 'unknown'
.
This issue seems to be linked to the optional nature of TypeA
and TypeB
generics, although they remain optional even in manual usage, so what exactly is going on here?
Is there a way to define ArgsType
based on optional generics and utilize it in my method declarations?