Typescript offers all the features of JavaScript, plus an additional layer on top of these – the Typescript type system. In this learning path, you’ll learn how using Typescript for JavaScript development can help you build more robust code, reduce runtime type errors, take advantage of modern features before they are available in JavaScript, and work better with development teams.
Prerequisites
- Familiarity with basic HTML and JavaScript ES6/2015, including:
- Conditionals
- Functions
- Scope
- Arrays
- Loops
- Iterators
- Objects
- Classes
- Modules
- Installed software:
- Git
- Visual Studio Code
- Node.js
- TypeScript
Modules in this learning path
Module 1: Get started with TypeScript
What is the relationship between TypeScript and JavaScript?
- TypeScript is a superset of JavaScript.
- TypeScript is exactly the same as JavaScript.
- TypeScript is a subset of JavaScript.
Why is it necessary to compile (or transpile) TypeScript code to JavaScript before you can use it in your applications?
- It’s not necessary because you only need to rename the TypeScript file with a .js extension for it to work.
- TypeScript code is considered valid JavaScript while JavaScript code isn’t valid TypeScript.
- TypeScript includes code features that aren’t compatible with browsers.
What does the command npm install -g typescript
do?
- Installs npm so you can install TypeScript.
- Installs the TypeScript compiler globally on the machine.
- Installs Node.js and npm.
Module 2: Declare variable types in TypeScript
Knowledge check
Choose the best response for each of the following questions. Then select Check your answers.
The boolean
, number
, string
, and enum
types are examples of which category of subtype of any
?
- Type parameters.
- Object types.
- Primitive types.
Which of the following types is an example of an object type?
Array
.void
.- Type parameters.
What is the primary difference between the any
and unknown
types?
- You can assign any value to
unknown
, but theany
type has some constraints. - You can access the properties of an
unknown
type but not anany
type. - You can access the properties of an
any
type but not anunknown
type.
What is the name of the feature in TypeScript that tells the compiler I know what I’m doing?
- Literal narrowing.
- Type assertion.
- Type guard.
What is a Tuple?
- An array with an infinite number of elements of the same type.
- An array with specific number of elements of the same type.
- An array with a specific number of elements of one or more types.
Module 3: Implement interfaces in TypeScript
Knowledge check
What is the primary job of an interface?
- Describe the properties and return types of an object.
- Define the implementation details for an object.
- Fulfill the code contract for an object.
How can you prevent the type system from raising an error when a property in an interface is omitted?
- Make the property optional.
- Make the property required.
- Make the property read only.
What happens when you extend one interface with another interface?
- Multiple interfaces can have the same property if the property has the exact same name.
- If two interfaces have a property with the same name but different types, TypeScript ignores the property completely.
- You must implement all the required properties from all interfaces.
Module 4: Develop typed functions by using TypeScript
Knowledge check
Choose the best response for each of the questions below. Then select Check your answers.
What is a difference between function parameters in TypeScript and function parameters in JavaScript?
- TypeScript parameters are required by default, but can be made optional. JavaScript parameters are always optional.
- TypeScript parameters are the same as JavaScript parameters.
- TypeScript parameters are always optional. JavaScript parameters are required by default but can be made optional.
What is a common use for an anonymous function?
- When you need to apply the same function signature to more than one function.
- When you need to be able to reuse the function throughout your code.
- When you need to assign a function expression to a variable.
How should you define a function type if you need to extend it?
- You can use either an interface or a type alias. They both work the same way.
- Incorrect. While both approaches work essentially the same, there are some differences between them.
- Define it with an interface.
- Define it with a type alias.
Module 5: Declare and instantiate classes in TypeScript
Knowledge check
How many constructor
functions can you include in a class definition?
- One.
- Any number.
- None.
What happens if you omit the get
accessor for a class property?
- You will not be able to set its value from your code.
- Nothing. TypeScript automatically provides
get
accessor functionality if it is omitted. - You will not be able to return its value from your code.
Which access modifier should you use if you don’t want a method to be accessible from code outside the class but also want to make it available inside other classes that derive from the class?
readonly
protected
private
When extending a class, what is the purpose of the super
keyword?
- It initializes the properties in the subclass.
- It overrides the properties of the base class.
- It executes the
constructor
of the base class when it runs.
Module 6: Define generics in TypeScript
Knowledge check
Choose the best response for each of the questions below. Then select Check your answers.
When using generics, you can limit the range of types that a type variable can accept. What is this called?
- A generic constraint.
- Type limiting.
- Generic limits.
What’s the best type of operation to use with a generic?
- Any operation is compatible with a generic.
- Mathematical operations.
- Operations that can be performed on a variety of data.
What’s the difference between using a generic and the any
type?
- The
any
type is very flexible so it’s preferred over generics in most cases. - Using a generic provides type checking while using the
any
type does not. - There is no difference between using a generic and using the
any
type.
Module 7: Access external libraries from TypeScript
Knowledge check
Which of the following is possible when using namespaces, but not when using modules?
Designate a component as available outside of the scope of the namespace using the export
keyword.
- Designate a component as available outside of the scope of the namespace using the
export
keyword. - Compile multiple TypeScript files into a single JavaScript file.
- Declaring dependencies.
How do you use use a component from one module file in another module file?
- Use the
import
keyword. - Use a
reference
statement. - Prepend the module name to the component name.
What is the recommended code-organizing mechanism for new ES6-compliant projects?
- Namespaces.
- It doesn’t matter which one you choose.
- Modules.
Module 8: Organize code using TypeScript namespaces
Knowledge check
What happens when you add code to a namespace?
- Declarations are removed from the global namespace..
- Declarations are removed from the global namespace only if the namespace is in a separate file.
- Declarations contribute to ‘global scope pollution’.
How do you make a component available outside the scope of a namespace?
- Add the
import
keyword to the file that will use the component. - Add the
export
keyword to the declaration. - Add a
reference
statement to the file that will use the component.
What is the option that tells the TypeScript compiler to output multiple files using and containing namespaces into a single JavaScript file?
--multifile
--outfile
- No option is required. The Typescript compiler does this operation by default.