TypeScript Flashcards
Why are “Types” useful and offer an advantage compare to vanilla JavaScript?
Types allow you to detect errors early and avoid some runtime errors
Will the following code throw a compilation error?
let userName: string;
userName = ‘Maximilian’;
userName = false;
Yes - assigning a boolean to a variable which was assigned a “string” types is not allowed and will yield a compilation error.
Does this code rely on type inference?
const age: number = 29;
Not really - a type is assigned explicitly as well. (TypeScript would be able to infer the type (and hence you should omit “:number”) but here, we actually also have an explicit type assignment)
What’s the difference between JavaScript types (e.g. typeof ‘Max’ => ‘string’) and TypeScript types (e.g. const name: string = ‘…’)?
TS types are checked during compilation (development) JS types are checked at runtime (JS has no compilation step but at runtime, you can check for certain types (e.g. in if conditions). TS on the other hand allows you to catch certain errors during development since it checks types during compilation as well.)
What are the core types of TS?
number, string, boolean, Object, Array, Tuple, Enum, Any, Union Types, Literal Types
What is the Unary plus (+) operator?
precedes a variable and attempts to convert it to a number
What is an IDE?
IDE stands for integrated development environment (VS Code)
Which of the following snippets could be simplified by using an enum type?
A)
const users = [‘Max’, ‘Michael’, ‘Julie’];
B)
const userA = { name: ‘Max’ };
const userB = { name: ‘Michael’ };
C)
const ROLE_ADMIN = 0;
const ROLE_AUTHOR = 1;
C
Will the following code throw a compilation error?
type User = {name: string; age: number};
const u1: User = [‘Max’, 29];
YES, the “User” type clearly wants an object with a “name” and an “age” property. NOT an array.
Will this code make it through compilation?
type Product = {title: string; price: number;};
const p1: Product = { title: ‘A Book’, price: 12.99, isListed: true }
NO. isListed is not part of the “Product” type
Will this code make it through compilation?
type User = { name: string } | string;
let u1: User = {name: ‘Max’};
u1 = ‘Michael’;
This code is fine. The union type allows either an object (with a “name” property) OR a string. You can switch values how often you want.
Will this code compile?
function sendRequest(data: string, cb: (response: any) => void) {
// … sending a request with “data”
return cb({data: ‘Hi there!’});
}
sendRequest(‘Send this!’, (response) => {
console.log(response);
return true;
});
YES.
That’s correct. As you learned, callback functions can return something, even if the argument on which they are passed does NOT expect a returned value.
What’s the idea behind a “function type”?
Function types define parameters and a return type of a function
Which code snippet is better (i.e. which code should you write)?
1)
function sayHi(): void {
// …
}
OR
2)
function sayHi(): undefined {
// …
}
1 because it doesn’t force you to return anything if you don’t want to return something