[Pick Up TS](3) 函数的类型兼容

341 阅读1分钟
let x = (a: number) => 0;
let y = (b: number, s: string) => 0;

x = y; // Error
y = x; // OK
let x = () => ({name: "Alice"});
let y = () => ({name: "Alice", location: "Seattle"});

x = y; // OK
y = x; // Error, because x() lacks a location property

如果函数A的参数完全能cover函数B的参数,那么B可以赋值给A,反之不可。

比如函数y的参数能完全cover函数x的参数,那么x可以赋值给y

如果函数A的返回值的能完全cover函数B的返回值,也就是A的返回值是B的返回值的子类,那么A可以赋值给B,反之不可。

比如y函数的返回值是x函数返回值的子类,那么y就可以赋值给x。

参数:多的可以赋值给少的返回值:少的可以赋值给多的

瓦滴脑子坏掉佬......