TypeScript Flashcards
Cover different topics in TypeScript.
ECMA Script 2015 introduced which two new keywords for declaring variables, and constants ?
let , const
What is the main difference between declaring a variable using keyword “var” or using keywords “let” or “const” ?
The main difference is in the scoping rules applied to the identifiers declared with them. Variables using “var” are globally available within the function they were declared in, while variables declared with “let” and “const” are local to the block they were declared in.
How can one identify a “block” ?
Usually a block is defined by a set of curly braces.
What is another difference between variables declared with “var” and variables declared with “let” and “const” ?
Variables declared with “var” are hoisted to the top of the function they were declared in, meaning, the run time behavior will be as if it was declared on the top even if it was declared on the bottom of a function. Variables declared with “let” and “const” are only available where they were declared.
What is the main difference between declaring a variable using keyword “var” or using keywords “let” or “const” ?
When using “var” , it is possible to declare the same variable name twice in the same function. This is usually not good practice, and with “let” and “const” you can only declare one name per block, avoiding this problem all-together.
What are the different “types” used in TypeScript ?
Boolean, Number, String, Array, Tuple, Enum, Any, Void, Null, Undefined, and Never.
What is a boolean’s value ?
True or false .
let isDone: boolean = false;
What is a number’s value ?
Any integer. It is not treated like a string !
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
What is a string’s value ?
Anything between “ “ .
let color: string = “blue”;
color = ‘red’;
What is an array’s value ?
A collection of more then 1 of a certain type .
let list: number[ ] = [1, 2, 3];
–this is a more generic way–
let list: Array = [1, 2, 3];
What is a Tuple’s value ?
Tuple types allow you to express an array where the type of a fixed number of elements is known, but need not be the same. For example, you may want to represent a value as a pair of a string and a number:
// Declare a tuple type let x: [string, number]; // Initialize it x = ["hello", 10]; // OK // Initialize it incorrectly x = [10, "hello"]; // Error
What is a Enum’s value ?
An enum is a way of giving more friendly names to sets of numeric values.
enum Color {Red, Green, Blue};
let c: Color = Color.Green;
Enums begin numbering their members starting at what ?
Enums start numbering at 0, but you can change that by manually setting the value of one of its members, or all of its members.
enum Color {Red = 1, Green = 2, Blue = 4};
let c: Color = Color.Green;
What is a handy feature of Enums ?
You can also go from a numeric value to the name of that value in the enum. For example, if we had the value 2 but weren’t sure what that mapped to in the Color enum we could look up the corresponding name:
enum Color {Red = 1, Green, Blue};
let colorName: string = Color[2];
alert(colorName);
What is Any’s value ?
It’s any value ! Allowing you to gradually opt-in and opt-out of type-checking during compilation.
What is Void’s value ?
the opposite of any: the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value:
function warnUser(): void { alert("This is my warning message"); }
What is Null and Undefined ?
By default null and undefined are subtypes of all other types. That means you can assign null and undefined to something like number.
What is Never’s value ?
The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns.
How do you assign a type to a variable ?
: type // after the variable
What happens if you don’t assign a type ?
TypeScript will infer one based on how you used the variable.
How do you assign a type to the return value of a function ?
: type // after the ) of the function parameters.