- Angular UI Development with PrimeNG
- Sudheer Jonna Oleg Varaksin
- 188字
- 2021-07-15 17:32:55
Type inference
Type inference is used when the type is not provided explicitly. For instance in the following statements:
var x = "hello";
var y = 99;
These don't have explicit type annotations. TypeScript can infer that x is a string and y is a number. As you see, the type can be omitted if the compiler is able to infer it. TypeScript improves the type inference continuously. It tries to guess a best common type when elements of several types are present in an array. The type of the following variable animal, where Sheepdog extends Dog, is Dog[]:
let animal = [new Dog(), new Sheepdog()];
The best common type of the next array is (Dog | Fish)[] because the class Fish doesn't extend to any other class:
class Fish {
kind: string;
}
let animal = [new Dog(), new Sheepdog(), new Fish()];
Type inference is also used for functions. In the next example, the compiler can figure out the types of the function's parameter (string) and the return value (boolean):
let isEmpty: (param: string) => boolean;
isEmpty = function(x) {return x === null || x.length === 0};
- HornetQ Messaging Developer’s Guide
- C語言程序設計(第3版)
- Raspberry Pi Networking Cookbook(Second Edition)
- Learn Scala Programming
- 精通API架構:設計、運維與演進
- Mastering Predictive Analytics with Python
- Java Web程序設計任務教程
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- C語言程序設計
- Java7程序設計入門經典
- Java 9 with JShell
- Learning Image Processing with OpenCV
- Learning Kotlin by building Android Applications
- 例說FPGA:可直接用于工程項目的第一手經驗
- 趣學數據結構