While creating this:
class AB<Initial, Current = Initial> {
array: AB<unknown, unknown>[] = []
push<Next extends any>(ab: AB<Current, Next>) : AB<Initial, Next> {
this.array.push(ab)
return this as AB<Initial, Next>;
}
}
// In this case, ab is AB<'a','b'> and
// AB<'a','b'>.push<Next = 'x'>(ab: <Current,Next>) should expect AB<'b','x'>
const ab = new AB<'a', 'b'>();
// Since ab is AB<Initial = 'a', Current = 'b'>,
// the following ab.push<Next>'s parameter should
// expect AB<Initial = 'b', Current = Next> and
// AB<'e', 'x'> should raise an error because 'e' is not 'b'
const ax = ab.push(new AB<'e', 'x'>);
// Given that ax is AB<Intial = 'a', Current = 'x'>,
// the parameter for the subsequent ax.push<Next> should
// anticipate AB<Initial = 'x', Current = Next>, causing
// an error if AB<'f', 'z'> is used since 'f' doesn't match 'x'
const az = ax.push(new AB<'f', 'z'>);
I am anticipating Typescript to flag an error due to the mismatch in generic type constraint from the method.
While I have worked extensively with type constraints previously, I am unable to pinpoint a reason for this particular scenario.