Say I want to extend the Promise class with an overriden then()
method that does something before calling super.then()
:
class ExtendedPromise<T> extends Promise<T> {
then(...x: Parameters<InstanceType<typeof Promise<T>>["then"]>) {
console.log("Do something here");
return super.then(...x);
}
}
TypeScript complains that this override is not assignable to the same property in the base type because unknown
is not assignable to type TResult1 | TResult2
but how is that possible? I’m accepting the same parameters as the parent type and literally returning the return value of super, so the types should all be identical?
TS Playground link: https://www.typescriptlang.org/play/?#code/MYGwhgzhAECiAeAXApgOwCbPQBQE4HsBbASwmQB4AVAPmmSTXRjyNIpugG8AoaP6RAAs0ACgB0E+AC5o2MLjCFkKXBHIBJVBERhUwZJQCeABwqITyfADNZBEmSrVqAbQBEQtK4C61AJRdefiDgfC18EGQxEHwAcxFXABF8aAgiZUFiVBjXXwBuQKC+XGUAV1xUFJLTXDEPVHFJPILoAF9uFqA