In my method with 2 arguments, I want it to deduce a type from the 1st argument.
For instance, in this code snippet, I aim for the type T
of the function create_C<T>
to be inferred based on the firstArgument
, leading to the return type of create_C
being
C<type inferred from firstArgument>
interface C<T> {
firstArgument: A<T>;
secondArgument: (obj: any) => T
}
export interface A<T> {
type: T;
}
function create_C<T>(
firstArgument: A<T>,
secondArgument: (obj: any) => T
): C<T> {
return {
firstArgument,
secondArgument
}
}
However, in the following scenario, const c
is deduced as C<{ prop2: number }>
. I anticipate it to be interpreted as C<B>
, and an error should trigger, indicating that the return type of secondArgument
does not match type B
interface B {
prop1: string;
prop2: number
}
export class B_Component implements A<B> {
type: B = {
prop1: "",
prop2: 1
};
}
const c = create_C(
new B_Component(),
() => ({ prop2: 2 })
)
How can I ensure the compiler raises an error when the return type of secondArgument
does not align with type B
?
Access the Stackblitz editor here: https://stackblitz.com/edit/qqddsn