- Angular UI Development with PrimeNG
- Sudheer Jonna Oleg Varaksin
- 314字
- 2021-07-15 17:32:55
Type definition files
JavaScript programs written in native JavaScript don't have any type information. If you add a JavaScript library such as jQuery or Lodash to your TypeScript-based application and try to use it, the TypeScript compiler can find any type information and warn you with compilation errors. Compile-time safety, type checking, and context-aware code completion get lost. That is where type definition files come into play.
Type definition files provide type information for JavaScript code that is not statically typed. Type definition files ends with .d.ts and only contain definitions which are not emitted by TypeScript. The declare keyword is used to add types to JavaScript code that exists somewhere. Let's take an example. TypeScript is shipped with the lib.d.ts library describing ECMAScript API. This type definition file is used automatically by the TypeScript compiler. The following declaration is defined in this file without implementation details:
declare function parseInt(s: string, radix?: number): number;
Now, when you use the parseInt function in your code, the TypeScript compiler ensures that your code uses the correct types and IDEs show context-sensitive hints when you're writing code. Type definition files can be installed as dependencies under the node_modules/@types directory by typing the following command:
npm install @types/<library name> --save-dev
A concrete example for jQuery library is:
npm install @types/jquery --save-dev
Most of the time, you have the compile target ES5 (generated JavaScript version, which is widely supported), but want to use some ES6 (ECMAScript 2015) features by adding Polyfills. In this case, you must tell the compiler that it should look for extended definitions in the lib.es6.d.ts or lib.es2015.d.ts file. This can be achieved in compiler options by setting the following:
"lib": ["es2015", "dom"]
- Visual C++程序設計教程
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- Java加密與解密的藝術(第2版)
- Spring Boot+Spring Cloud+Vue+Element項目實戰:手把手教你開發權限管理系統
- Modular Programming in Java 9
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- Mastering Business Intelligence with MicroStrategy
- Java網絡編程實戰
- 一塊面包板玩轉Arduino編程
- Kotlin開發教程(全2冊)
- Troubleshooting Citrix XenApp?
- 一步一步跟我學Scratch3.0案例
- Drupal 8 Development:Beginner's Guide(Second Edition)
- 視窗軟件設計和開發自動化:可視化D++語言