We examine the basic types in TypeScript, and examine how variables are assigned a type, including type inference and explicit setting of the type.
- [Instructor] Let's start at the very beginning and discuss types in TypeScript. Var variables are scoped to the function. If there's no function they're declared globally. To overcome this craziness use let instead of var. Let is scoped to the block, as all good variables should be. Const is also scoped to the block. Let's begin by taking a look at some of the most important types. We'll create a new file and we'll call that stringdemo.ts, and that .ts is very important.
It indicates to VS Code that this is a TypeScript file. As you can see in the lower right hand corner it says Type Script. We're going to create a function called string demo and that function is going to take one string parameter. We'll call that parameter what to say and we declare its type by putting a colon and then the type, in this case string. So string demo takes one parameter what to say.
Inside of string demo, our function, we're going to write to the console. So we're going to say console.log and we'll pass to console.log our string parameter what to say. Outside of our function we can invoke it, sting demo and pass in any string. We'll pass in the canonical string hello world. We now need to build string demo.ts.
On the Mac that's shift command B. On Windows it's shift control B. Let's go over to the terminal and we can now run that by typing node string demo. And there we see our string hello world. Let's take one second and look at what's inside this folder. Let's go back to VS Code, close string demo. Next we're going to create a new file to look at numbers.
Let's call that numberdemo.ts, and we'll create a new function number demo and we're giving it the same name as the ts file, that's not required. The function is going to take two parameters, both of type number. The first will be age and the way we indicate that its type is number is with colon and then number the type, and the second parameter will be length of employment, and that too will be a number.
In the function itself we'll write to the console, and we do that with console log, and we want to use substitution parameters. So we're going to put a backtick. That's not a single quote it's a backtick, which is usually in the upper left hand corner of your keyboard. You are, and here's our first substitution parameter, dollar sign brace and we'll put in the parameter age, years old, and you have worked here, and our second substitution parameter length of employment years.
Let's close off our log and a semicolon to end it. We now have a complete function numbers demo. We can invoke that function by saying number demo and passing in two numbers, the first to the age, let's put 21 and the second for length of employment, a number. We're going to put that you've been there for four and three quarter years, that is 4.75. Once again notice that in C Sharp we would use an integer and a double or a float both of these are numbers in TypeScript.
With that all in place we're ready to build shift command B on the Mac, shift control B on Windows. Let's go over to the terminal and say node number demo and sure enough our substitution parameters worked and our numbers played out as we expected, 21 years old and 4.75 years. Now that we've look at number and string, probably the two most used types in TypeScript let's go on and take a look at some of the other basic TypeScript types.
In this course, Jesse Liberty reviews the fundamentals of TypeScript for the C# developer, including the built-in types, flow controls, and functions. He covers default, optional, and rest parameters; lambda functions; object literals; and the creation and use of custom classes. Plus, learn about other object-oriented features such as inheritance and interfaces. By the end of the course, C# developers should be well on their way to incorporating this flexible and powerful web programming language into their app development workflow.
- Setting up a TypeScript development environment
- Working with types and variables
- Using operators to control flow
- Working with parameters and functions
- Creating classes and objects
- Exploring TypeScript inheritance
- Working with interfaces