Essentially the configuration of a compatibility check is the current coverage property attribute data to target data.
1. compatibility check interface
// when the actual property contains all the data types of properties of the target, compatible interface Animal { username: String, Age: Number } interface the Person { username: String, Age: Number, Married: Boolean } the let B: the Person = {username : 'Lee', Age:. 1, Married: to false }; function getName (Val: Animal) { return val.username; } // but contains the parameters corresponding to the type Person Animal all attributes; // compatible getName (b) ;
2. Basic type compatibility check
// 1. Compatibility basic types of the let All: String | Number; the let STR: String = "Hello World" ; All = STR; // 2. Compatibility basic types of the let A1: { toString (): String }; A2 the let: String = 'Hello' ; A1 = A2;
3. Compatibility class
AnimalNew {class public name: String = 'Lee' ; } class Dog the extends AnimalNew { public food: String = 'Bone' ; } the let Dog: Dog; Dog = new new AnimalNew (); // ❌Animal food does not contain attributes let ani : AnimalNew; ANI = new new Dog (); // ✅Dog Animal type attribute comprising
4. Compatibility Function
Function parameters need compatibility comparison function required current parameter is a subset of the objective function parameter.
They are then asked to return values coincide.
type sumFn = (a: number, b: number) => number let sum:sumFn; let sum1 = function(a: number):number { return a; } sum = sum1;
If the type parameter of the objective function is more optional type, then the current optional type parameter contains optional functions required by the type of objective function parameters.
type addFn = (a: number|string) => void; let add: addFn; add = function(b: number) {}; /* ❌Types of parameters 'b' and 'a' are incompatible. Type 'string' is not assignable to type 'number'. */ add = function(a: number|string|boolean) {}; // ✅
The generic compatible
Generic interface. When the interface is empty, it can be compatible. When the content is not empty, the different types are incompatible.
interface User<T> { } let x:User<number> = {}; let y:User<string> = {}; y = x; //✅ interface Name<T>{ n: T } let n:Name<number> = {n: 1}; let m:Name<string> = {n: '1'}; n = m; // ❌ 不兼容
6. Type enum type and values compatible with each other
enum Colors{ RED, BLUE } let c:Colors; c = 0; let c1:number; c1 = Colors.RED;