I recently upgraded to typescript 2.4 and I got several errors complaining that my types were no longer assignable.
Here is the scenario where I am encountering the error:
interface Parent {
prop: any
}
interface Child extends Parent {
childProp: any
}
type Foo<T> = <P extends Parent>(parent: P) => T
function createFooFunction<T>(arg: T): Foo<T> {
// Error here!
return (child: Child): T => {
return arg;
}
}
In typescript 2.3 this is acceptable, but typescript 2.4 produces this error
Type '(child: Child) => T' is not assignable to type 'Foo<T>'.
Types of parameters 'child' and 'parent' are incompatible.
Type 'P' is not assignable to type 'Child'.
Type 'Parent' is not assignable to type 'Child'.
Property 'childProp' is missing in type 'Parent'.
In regards to the last line of the error, I noticed that if I make the Child's properties optional, then typescript will be satisfied, i.e. if I make this change
interface Child extends Parent {
childProp?: any
}
Although that is not an ideal fix, since in my case, childProp is required.
I also noticed that changing the Foo's argument type directly to Parent will also satisfy typescript, i.e. making this change
type Foo<T> = (parent: Parent) => T
This isn't a fix either though, since I do not control the Foo type, nor the Parent. They both come from vendor .d files, so I cannot modify them.
But either way, I'm not sure I understand why this is an error. The Foo type is saying that it requires something that extends Parent, and Child is such an object, so why would typescript think its not assignable?
Edit: I've marked this as answered since adding the --noStrictGenericChecks flag will suppress the errors (accepted answer here). However, I would still like to know why this is an error in the first place since I'd rather keep the strict checks and refactor my code if its wrong, rather than just short circuiting it.
So to re-iterate the heart of the question, since Child extends Parent, why does typescript no longer think that Child is assignable to Parent, and in terms of OOP generics, why is that more correct than it was before?