typescript中兼容性记忆

60 阅读1分钟

typescript中兼容性记忆:

ts中兼容性指:A兼容B,表示可以把B赋值给A。

1.针对接口:

interface Animal { name: string; } 
let x: Animal; 
let y = { name: '旺财', type: 'dog' }; 
x = y;

y可以赋值给x,表示x兼容y。

记忆:接口(字段)多的可以赋值给少的。 (子可以赋值给父).

2.针对函数:

let x = (a: number) => 0; 
let y = (b: number, s: string) => 0; 
y = x; // OK 
x = y; // Error

x可以赋值给y,表示y兼容x。

记忆:函数(参数)少的可以赋值给多的。