This content originally appeared on Level Up Coding - Medium and was authored by Maxwell
Function Definition, Classes and Inheritance in TypeScript
TypeScript Learning Notes I: Installing Compilation and Basic Types
Function Definition
Let’s start with some easy-to-understand example to explain several ways to define functions in TypeScript.
- Function declaration method
2. Anonymous function method
3. Define methods to pass parameters, with the type declared for both the parameters and the method name.
4. Anonymous functions passing parameters with declared types for both parameters and method names.
5. Methods with no return value.
6. In TypeScript, the formal and real parameters must be the same, if they are not the same, we must configure the optional parameters, add ? sign.
7. Setting default parameters in TypeScript.
8. Usage of ES6 residual parameters in TypeScript.
9. Function overloading in TypeScript, where two or more functions of the same name are overloaded and one of them is executed selectively depending on the number or type of arguments, resulting in a different result.
When there is a function with the same name in ES5, the function declared later will overwrite the function declared earlier, and in TS it is written as follows.
A variation of the above method can also be written as the following.
Classes and Inheritance
The method of class and inheritance in TypeSctipt is basically the same as in ES6 and is written as follows.
- Definition Class
2. Inheritance Class
3. The modifiers for classes in TypeScript, as in C++, are mainly as follows.
(1) public
It can be accessed inside the class, inside the subclass, and outside the class. Properties are public by default if no modifier is added.
(2) protected
It can be accessed inside the class and subclasses, but not outside the class.
(3) private
It can be accessed inside the class, but not in the subclass or outside.
This series of articles is dedicated to helping you get started with TypeScript with simple examples. if you are interested in my articles, you can follow me on Medium or Twitter.
Level Up Coding
Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the Level Up Coding publication
- 🔔 Follow us: Twitter | LinkedIn | Newsletter
🚀👉 Join the Level Up talent collective and find an amazing job
Making It Easier To Get Started With TypeScript was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Maxwell
Maxwell | Sciencx (2022-11-10T03:23:02+00:00) Making It Easier To Get Started With TypeScript. Retrieved from https://www.scien.cx/2022/11/10/making-it-easier-to-get-started-with-typescript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.