- 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};
- Data Visualization with D3 4.x Cookbook(Second Edition)
- 計(jì)算思維與算法入門
- 工程軟件開(kāi)發(fā)技術(shù)基礎(chǔ)
- 深入淺出Spring Boot 2.x
- Data Analysis with Stata
- 實(shí)戰(zhàn)Java高并發(fā)程序設(shè)計(jì)(第3版)
- 西門子S7-200 SMART PLC編程從入門到實(shí)踐
- Couchbase Essentials
- Kivy Cookbook
- OpenCV 3計(jì)算機(jī)視覺(jué):Python語(yǔ)言實(shí)現(xiàn)(原書(shū)第2版)
- App Inventor少兒趣味編程動(dòng)手做
- Vue.js光速入門及企業(yè)項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)
- 高質(zhì)量程序設(shè)計(jì)指南:C++/C語(yǔ)言
- Extending Docker
- 分布式系統(tǒng)架構(gòu)與開(kāi)發(fā):技術(shù)原理與面試題解析