- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 203字
- 2021-07-15 17:05:31
Function type interfaces
We just saw how to define properties in interfaces. Similarly, we can also define function types in interfaces. We can define function types in interfaces by just giving the signature of the function with the return type. Note that, in the following code snippet, we have not added the function name:
interface AddCustomerFunc { (firstName: string, lastName: string): string; }
Now, we have AddCustomerFunc ready. Let's define an interface variable called AddCustomerFunc and assign a function of the same signature to it as follows:
var addCustomer: AddCustomerFunc; addCustomer = function(firstName: string, lastName: string) { console.log('Full Name: ' + firstName + ' ' + lastName); return firstName + ' ' + lastName; }
The parameter name in the function signature can vary, but not the data type. For example, we can alter the fn and ln function parameters of the string type as follows:
addCustomer = function(fn: string, ln: string) { console.log('Full Name: ' + fn + ' ' + ln); }
So, if we change the data type of the parameter or the return type of the function here, the compiler will throw an error about the parameter not matching or the return type not matching with the AddCustomerFunc interface.
- 大話PLC(輕松動漫版)
- 自然語言處理實戰:預訓練模型應用及其產品化
- Java 11 Cookbook
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- 深入RabbitMQ
- Learning Three.js:The JavaScript 3D Library for WebGL
- Lighttpd源碼分析
- Essential C++(中文版)
- Orchestrating Docker
- Python青少年趣味編程
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器
- 數據科學中的實用統計學(第2版)
- TypeScript全棧開發
- Kotlin語言實例精解
- jBPM6 Developer Guide