3 ms·
I believe that `strictFunctionTypes` only applies to callbacks. TypeScript is okay with the following code even with that option turned on: class Animal {}
by kevinb7 7y ago
I believe that `strictFunctionTypes` only applies to callbacks. TypeScript is okay with the following code even with that option turned on:
class Animal {}
class Cat extends Animal { meow() {} }
class Dog extends Animal { woof() {} }
function foo(animals: Animal[]) {
animals.push(new Dog)
}
const cats: Cat[] = [new Cat];
foo(cats);
https://www.typescriptlang.org/play/#code/MYGwhgzhAECCB2BLAtmE0DeBfAUKSMAwmAC7QCmAHiefACYwIpqbTLkD2A7gBQCUmLNFz4o0ACIcA5hWq0GcJKnQZoXDhwBm-QcJw5NAV3jASiDvGiaNPMErQQAXIuYgA2gF0BGHNGh3XCAA6AAdDCAALHnhyLglpPhxcPAsIMmBSJ2hiEk9oAF5oNxi4nI8Abn1rDh4Mkgg+SqA https://www.typescriptlang.org/play/#code/MYGwhgzhAECCB2BLAt...
- Arnavion 7y agoYour example is identical to the case I covered in the second paragraph of my comment. ie your code compiles because it is legal to assign a `Cat[]` to an `Animal[]`, as I said. When you mentioned "covariant param passing" I assumed you were referring to the long-lasting issue about callbacks, hence my original comment mentioning that that issue has been fixed. It's not useful to differentiate between "assigning a value to a variable of a certain type" and "passing a value to a function parameter of a certain type" because those are identical.