Introduction to Static Typing

Ari Picker
InstructorAri Picker
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

This is a basic introduction to static typing with Typescript. In this lesson we will learn about simple types in TypeScript.

[00:00] TypeScript is JavaScript with static typing. Basically the compiler knows what the variable's type is during compilation. To put it another way, types are associated with variables, not values. This is a type annotation for a string, and when I run the compiler, I get no errors.

[00:18] Now let's see how static typing is useful by updating the sum string variable to a different type than it's expecting. Let's run a compiler. The compiler is mad because I tried to assign a number to a variable that I already said was a string. Sum string is a string. I gave it a number.

[00:37] Please note that even though the compiler is mad, it doesn't prevent compilation in most cases. This is because TypeScript is still JavaScript, and JavaScript types are dynamic. Basically, JavaScript doesn't care what type your variable is, but you will if it's the wrong one.

[00:51] Another useful aspect of TypeScript is exploring APIs with an IDE is way easier. The [indecipherable] function accepts a string and a number. When we begin typing the function, the IDE helps with auto-complete, but also shows us what type of arguments the function is expecting. You can see the first one is a string, and the second one is a number. When I run the compiler, no errors.

[01:18] If we pass an argument that the function is not expecting, the IDE will warn us. You can see there's a little red line right here, and there's also one on the side over here. The error that it's pointing out will also show up when you compile. I tried to pass an object to the function that's supposed to be a number.

Michal
Michal
~ 8 years ago

Thanks egghead for that series:). Fantastically would be to see series about using typescript in AngularJS or React.js.

Michal
Michal
~ 8 years ago

I've already used typescript in React. But, still I think use it in medium size Angular app would be harder than in a small React app.

Markdown supported.
Become a member to join the discussionEnroll Today