My goal is to create a base class in TypeScript that accepts a union of string types as a generic type, and then uses these strings as keys in an abstract method that utilizes a dictionary.
The issue I'm encountering is that the child implementation of the abstract method does not throw a type error when additional properties are passed to it (even though it correctly throws an error if properties are missing):
abstract class Parent<T extends string = string> {
abstract doSomethingWithT(options: { [key in T]: string }): void;
}
class Child extends Parent<"foo" | "morefoo"> {
/**
* Should trigger an error on 'bar'
*/
doSomethingWithT(arg: { foo: string; morefoo: string; bar: string }) {
}
}
I am searching for a solution to enforce this behavior in Typescript.