Chapter 1: TypeScript Basics

  1. What are the five primitive types?
  • string: Represents a sequence of Unicode characters
  • number: Represents both integers and floating-point numbers
  • boolean: Represents a logical true or false
  • undefined: Represents a value that hasn't been initialized yet
  • null: Represents no value
  1. What will the inferred type be for the flag variable be in the following code?
 const flag = false;

flag will be inferred as the boolean type.

  1. What's the difference between an interface and a type alias? 

The main difference is that type aliases can't be extended or implemented from, like you can with interfaces.

  1. What is wrong with the following code?
class Product {
constructor(public name: string, public unitPrice: number) {}
}

let table = new Product();
table.name = "Table";
table.unitPrice = 700;

The constructor requires name and unitPrice to be passed. Here are two ways to resolve the problem.

Pass the values in the constructor:

let table = new Product("Table", 700);

Make the parameters optional:

class Product {
constructor(public name?: string, public unitPrice?: number) {}
}
  1. If we want our TypeScript program to support IE11, what should the --target compiler option be?

This should be es5 because IE11 only supports up to ES5 features.

  1. Is it possible to get the TypeScript compiler to transpile ES6 JavaScript files? If so, how?

Yes! We can use the --allowJS setting to get the compiler to transpile JavaScript files. 

  1. How can we prevent console.log() statements from getting into our code? 

We can use tslint and the "no-console" rule to enforce this. This would be the rule in tslint.json:

{
"rules": {
"no-console": true
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.106.100