The course is part of this learning path
This module will introduce you to Simple Types in TypeScript. You’ll learn about Arrays, Tuples, and Type Assertion, too.
The objectives of this module are to provide you with an understanding of:
- What Simple Types are
- How to use Arrays
- How to use Tuples
- How to use Type Assertion
This learning path is aimed at all who wish to learn how to use TypeScript.
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org to let us know what you think.
Tuples in TypeScript allow arrays of mixed types. The types and their order must be defined on declaration. However we can use union notation, that's bars, to allow different defined types in an element. We can use a question mark at the end of a type to allow the population of the element to be optional. All elements following an optional element must be optional too. Accessing elements in a tuple is the same as for arrays using the index, as is adding to and removing with a subtle difference as we'll see. So here on line 11 we have a tuple to store a string then two numbers and finally an optional number or a string. We can define the values for the tuple as long as we follow the types and order we specify, including the different optional types. On line 20 if we try and change the order then we get errors shown and there'll be a compilation error if we don't fix it. The errors are assign errors. We're still allowed to add and remove from a tuple as long as we add one of the types that's already been declared. If we try to push a non-declared type, as on line 30, we'll get an ID error again which tells us that boolean is not assignable to string or number. What would happen if we ignore the compiler error? We should remember the only lesson we need for TypeScript. We've written valid Java script, so compiling this code makes a valid Java script file but it's now less type safe than if we taken heed of the TypeScript errors. This is the whole reason we might have chosen to use TypeScript in the first place, to make our code more type safe and more robust. De-structuring a tuple is slightly different to that in arrays as the type is transferred for each element. If we declare a tuple as number string boolean with values and then use normal de-structuring, the values and types follow the de-structured variables. A will be of type number and value seven. B will be of type string and value hello. And C will be of type boolean and value true. Trying to change a value that does not match the de-structured type gives an error. As will de-structuring too many values as seen on line 11. Java script's spread up array can be used to get a shorter tuple, or have an empty tuple at the end of the de-structure. Elements can be ignored in as in arrays. This can be seen on line 18 and 19. So now you've seen how to use arrays and tuples why don't go ahead and get some practice?
Simple Types: An Introduction - Arrays - Any and Unknown - Null and Unidentified - Function Arguments and Return Types - Enums - Type Assertion
Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.
Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.