- Hands-On Machine Learning with JavaScript
- Burak Kanber
- 584字
- 2021-06-25 21:38:18
TypeScript language
The development and sharing of new packages on npm was not the only result of JavaScript's popularity. JavaScript's increasing usage as a primary programming language caused many developers to lament the lack of IDE and language tooling support. Historically, IDEs were more popular with developers of compiled and statically-typed languages such as C and Java, as it’s easier to parse and statically analyze those types of languages. It wasn't until recently that great IDEs started appearing for languages such as JavaScript and PHP, while Java has had IDEs geared towards it for many years.
Microsoft wanted better tooling and support for their large-scale JavaScript projects, but there were a few issues with the JavaScript language itself that got in the way. In particular, JavaScript's dynamic typing (the fact that var number could start its life as the integer 5, but then be assigned to an object later) precludes using static analysis tools to ensure type safety, and also makes it difficult for an IDE to find the correct variable or object to autocomplete with. Additionally, Microsoft wanted a class-based object-oriented paradigm with interfaces and contracts, but JavaScript's object-oriented programming paradigm was based on prototypes, not classes.
Microsoft therefore invented the TypeScript language in order to support large-scale JavaScript development efforts. TypeScript introduced classes, interfaces, and static typing to the language. Unlike Google's Dart, Microsoft made sure TypeScript would always be a strict superset of JavaScript, meaning that all valid JavaScript is also valid TypeScript. The TypeScript compiler does static type checking at compile time, helping developers catch errors early. Support for static typing also helps IDEs interpret code more accurately, making for a nicer developer experience.
Several of TypeScript's early improvements to the JavaScript language have been made irrelevant by ECMAScript 2015, or what we call ES6. For instance, TypeScript's module loader, class syntax, and arrow function syntax have been subsumed by ES6, and TypeScript now simply uses the ES6 versions of those constructs; however, TypeScript still brings static typing to JavaScript, which ES6 wasn't able to accomplish.
I bring up TypeScript here because, while we won't be using TypeScript in the examples in this book, some of the examples of ML libraries we examine here are written in TypeScript.
For instance, one example found on the deeplearn.js tutorials page shows code that looks like the following:
const graph = new Graph();
// Make a new input in the graph, called 'x', with shape [] (a Scalar).
const x: Tensor = graph.placeholder('x', []);
// Make new variables in the graph, 'a', 'b', 'c' with shape [] and
random
// initial values.
const a: Tensor = graph.variable('a', Scalar.new(Math.random()));
const b: Tensor = graph.variable('b', Scalar.new(Math.random()));
const c: Tensor = graph.variable('c', Scalar.new(Math.random()));
The syntax looks like ES6 JavaScript except for the new colon notation seen in const x: Tensor = … : this code is telling the TypeScript compiler that the const x must be an instance of the Tensor class. When TypeScript compiles this code, it first checks that everywhere x is used expects a Tensor (it will throw an error if not), and then it simply discards the type information when compiling to JavaScript. Converting the preceding TypeScript code to JavaScript is as simple as removing the colon and the Tensor keyword from the variable definition.
You are welcome to use TypeScript in your own examples as you follow along with this book, however, you will have to update the build process that we set up later to support TypeScript.
- 32位嵌入式系統(tǒng)與SoC設(shè)計導論
- 輕輕松松自動化測試
- PowerShell 3.0 Advanced Administration Handbook
- 程序設(shè)計缺陷分析與實踐
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·工業(yè)機器人
- INSTANT Drools Starter
- Practical Big Data Analytics
- INSTANT Heat Maps in R:How-to
- 工業(yè)機器人實操進階手冊
- Puppet 3 Beginner’s Guide
- 網(wǎng)絡(luò)安全概論
- 網(wǎng)站規(guī)劃與網(wǎng)頁設(shè)計
- 天才與算法:人腦與AI的數(shù)學思維
- Wireshark Revealed:Essential Skills for IT Professionals
- 巧學活用Linux