Trying to force a method with a generic parameter to have a union type. The issue is that TS is fine with the implementation of this method to only satisfy the union, rather than actually be the union. I might be misusing the satisfy terminology here, but I hope my point comes across clearly in the example below.
interface IFoo<TArg> {
myMethod(arg: TArg): void;
}
class Foo implements IFoo<string | number> {
// TS doesn't complain because string satisfies string | number.
// How can I make TypeScript force arg to explicitly be string | number?
myMethod(arg: string): void {
//
}
}
class Foo2 implements IFoo<string | number> {
// What I want TS to enforce:
myMethod(arg: string | number): void {
//
}
}
I messed around with tuples and tried looking for a helper type that could solve this to no avail.
edit: Link to playground: https://tsplay.dev/wOx5EN