Introduction to Static Typing

    Ari PickerAri Picker

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



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    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.