Type inference
That does not require the type specified variable, TS compiler can infer the type automatically according to certain rules.
When does type inference?
- When you declare a variable type is not specified
- Default function arguments
- Function return value
- ......
let a; // time automatically inferred to any type let b = 1; // number inferred type let c = []; // inferred by any type of array configuration let d = (x = 1) => x +1; // the transfer function parameters, default parameters are inferred as the type number, the return value will be inferred let e = [1, null] ; // deduce all compatible types of data: number and type of null combined with a when TS type inference does not meet our expectations, we should have more confidence in it than the compiler should be what type, type inference assertion allows us to cover the TS. {Foo interface bar: Number } the let foo: Foo Foo AS = {}; // if an object interface by convention, requires a lot of properties and methods, it is difficult to define exactly when it was declared. This time can be used to assert foo.bar = 1; // specific definition here
Note: The type of assertion can not be used indiscriminately, to have sufficient anticipation of context, without any basis assertion be a security risk!
Type compatibility
TS compatible allowed types of variables (function, structure etc.) assigned to each other.
When a type Y may be assigned to another type when X, Y considered compatible with the type X, the target type X, Y of the source type.
Compatible with the rules:
- Compatible between the structure: less compatible members and more members
- Incompatibility between functions: multi-parameter less compatible arguments
Interface Compatibility
interface X-{ A: the any; B: the any; } interface the Y { A: the any; B: the any; C: the any; } the let X: X-= {A:. 1, B: 2}; the let Y: the Y = {A :. 1, B: 2, C:. 3}; X = y; y // X compatible fewer members will be compatible with many members
Function Compatibility
// number of parameters compatible type Handler = (A: Number, B: Number) => void; function Hof (Handler: Handler) { return Handler } the let handler1 = (A: Number) => {}; Hof (handler1 ); // a parameter compatible with the let handler2 is = (a: Number, B: Number, C: Number) => {}; // Hof (handler2 is); // three parameters is incompatible // optional and the remaining parameters compatible with the let A1 = (P1: Number, P2: Number) => {}; the let B1 = (Number ?: P1, P2 ?: Number) => {}; the let C1 = (args ...: Number []) => {}; A1 = B1; A1 = C1; // A1 = B1; // optional parameter can not be compatible. Tsconfig.json in need "strictFunctionTypes" set to false // B1 = C1; C1 = A1; C1 = B1;
Compatibility class
Two different definitions of classes are not compatible, the subclass does not make changes after inheriting the parent class can be compatible with the parent class.
Generic Compatibility
Two definitions of exactly the same generic function compatible
Type of protection
In particular to ensure TypeScript block variables with some type of determination. You can safely access the properties and methods of this type in this block.
For example, we have to determine whether an object contains a method
{the OBJ interface name: String, Age: Number, sex: Boolean } the let obj: the OBJ = { name: "the typescript", Age: 10, sex: to true, }; IF (obj.sex) // obj property has sex Therefore the OK { the console.log ( "Sex has"); } // IF (obj.bac) {} // no obj bac attributes given here.
We have four types of protection provided by:
- instanceof for determining whether an instance of a class
- in determining a property / method belongs to an object
- for determining basic types typeof
- When it is determined when the type of the protecting function logic complexity, the judging function can customize
{the Java class HelloJava () { the console.log ( "Hello Java"); } Java: the any; } class the JavaScript { hellloJavaScript () { the console.log ( "Hello JavaScript"); } JavaScript: the any; } // Protection Type Note that the function parameter types and return value type of relationship function isJava (lang: the Java | JavaScript): iS the Java lang { return (the Java lang AS) .helloJava == undefined! } function getLanguage (of the type: Number the, the X-: String | Number the | boolean) { the let of the type === lang = 1 new new Java (): new new JavaScript ();? // lang instanceof to determine whether the Java class iF (lang instanceof Java) { lang.helloJava (); // in this block , it is possible to ensure a certain lang is an instance of java, call your own way the else} { lang.hellloJavaScript (); // this block, is a JavaScript necessarily guarantee Examples lang } // in "HelloJava" lang Object method whether IF ( "HelloJava" in lang) { lang.java; // in this block can be guaranteed, it is an example of the java lang } the else { lang.javascript // this block is guaranteed lang JavaScript examples } // typeof for basic types if (typeof x === "string ") { x.length this // program proceeds to block, to ensure that x is a string type. The method can be called primary string } the else IF (typeof X === "Number") { x.toFixed (2); // native methods herein can call digital } the else { X = X;! } // Protection Type when the function is defined, Note that the return type and parameter types of relationship value if (isJava (lang)) { lang.helloJava (); }else { lang.hellloJavaScript(); } } getLanguage(2,"str");