- Learn React with TypeScript 3
- Carl Rippon
- 254字
- 2021-06-10 19:16:31
Using future JavaScript features
There is another benefit of TypeScript that is important to understand. TypeScript allows us to use some features in JavaScript that haven't yet been adopted by all browsers but still target those browsers. TypeScript achieves this by transpiling the use of these features down to JavaScript code that the targeted browser does support.
As an example, let's look at the exponentiation operator (**) in ES7, which isn't supported in IE. Let's create a file called future.ts and enter the following code:
var threeSquared: number = 3 ** 2;
console.log(threeSquared);
When we run the program in a browser, it should put 9 into the console. Before we do that, let's run the code against the TypeScript compiler to get the transpiled JavaScript. Run the following command in a terminal in the same directory as future.ts:
tsc future
This should generate a file called future.js with the following content:
var threeSquared = Math.pow(3, 2);
console.log(threeSquared);
So, TypeScript converted the exponentiation operator to a call to the Math.pow function, which is supported in IE. To confirm that this works, paste the generated JavaScript code into the console in IE and the output should be 9.
This example is purposely simple but probably not that useful. Async/await, spread operators, rest parameters, and arrow functions are far more useful features that IE doesn't support but TypeScript allows the use of. Don't worry if you don't know what the features in the last sentence are, as we'll cover them when we need them in the book.
- Advanced Splunk
- Getting started with Google Guava
- Learning ArcGIS Pro 2
- Dependency Injection in .NET Core 2.0
- Servlet/JSP深入詳解
- Rust Essentials(Second Edition)
- INSTANT Django 1.5 Application Development Starter
- 從Excel到Python:用Python輕松處理Excel數(shù)據(jù)(第2版)
- Visual Basic程序設計習題與上機實踐
- Visual C++從入門到精通(第2版)
- WCF技術(shù)剖析(卷1)
- 實戰(zhàn)Python網(wǎng)絡爬蟲
- Python面向?qū)ο缶幊蹋ǖ?版)
- R語言:邁向大數(shù)據(jù)之路
- Learning Redis