- 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
- 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.
- 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.
- 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) {}
}
- 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.
- 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.
- 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
}
}