- 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.
- Java完全自學教程
- C#完全自學教程
- Getting Started with PowerShell
- Functional Programming in JavaScript
- R Data Analysis Cookbook(Second Edition)
- 組態(tài)軟件技術(shù)與應(yīng)用
- iOS開發(fā)實戰(zhàn):從入門到上架App Store(第2版) (移動開發(fā)叢書)
- C++語言程序設(shè)計
- 實驗編程:PsychoPy從入門到精通
- Joomla!Search Engine Optimization
- 算法超簡單:趣味游戲帶你輕松入門與實踐
- 你必須知道的.NET(第2版)
- Learn C Programming
- Scratch 3.0 趣圖探秘
- 計算機應(yīng)用基礎(chǔ)教程