I want to define class with generic parameter T and some methods operating with instances of the same class with the same parameter T.
But it doesn’t produce type errors as I expect.
Basically I want to something like this:
class C<T extends "a" | "b"> {
foo(b: C<T>): C<T> {
return b;
}
}
I want this to ensure safety, but typescript doesn’t produce an error on the last line as I expect.
a.foo
has type (C<"a">) => C<"a">
but accepts C<"b">
const a = {} as C<"a">;
const b = {} as C<"b">;
a.foo(b);
Simpler example without class in argument produces error.
class D<T extends "a" | "b"> {
foo(b: T): T {
return b;
}
}
const a = {} as D<"a">;
a.foo("b");