The Basics Flashcards
Using template literals, how do you output to console hello math if math is stored in a myName const?
const name = "math"; console.log(`Hello ${name}`);
Write a fully typed function called add that takes 2 parameters a and b and return the result of a + b
function add(a: number, b: number): number { return a + b; }
Using the arrow function notation write a fully typed function called add that takes 2 parameters a and b and return the result of a + b. The function has to be specific to the number type.
const add = (a: number, b: number): number => a + b;
Can a function be assigned to a const ?
Yes. For example:
const add = function (a: number, b: number): number {
return a + b;
};
Are function parameters required by default in Typescript ? Is it possible to call a function with 2 parameters with 1 or no parameter at all?
Yes they are required.
You can’t call a function with parameters without specifying any parameters.
For instance, calling :
function add(a: number, b: number) { return a + b; } With : add(3) // a single parameter we are missing b... //Will raise an error.
How can we make a parameter optional in Typescript?
By adding ? after the parameter. For example:
function doSomething(a: number, b?: number): number { //logic. We need to handle the fact that b might or might not be provided... }
What is the value of an optional parameter in Typescript when it’s not provided?
undefined
What is the nullish coalescing operator ?
??
What happens to Typescript types at runtime?
They don’t exist…
In Typescript what will be the type of:
let today = new Date(2023-01-01);
The type will be Date. This is a Typescript specific type that doesn’t exist in Javascript. In Javascript the type of today would be object.
What is the type of name in:
const name = “math”;
The type will be ‘math’ because Ts infers that this is a string constant… There is only one possible value and this is called a string literal type.
What is the type days in:
const days = 31;
31 a number literal
What is the type of today in:
const today = new Date(2023-01-01);
The type is Date a Typescript specific type.
What will be the type of nameAgain in:const name = "math";
const nameAgain = last;
The type will be ‘math’ a string literal.
What will be the type of nameAgain in:
const name = "math"; let nameAgain = name;
The type will be ‘math’ a string literal.
What will be the type of ourName in :
const myName = "math"; const yourName = "samantha"; const ourNames = `${myName} ${yourName}`;
The type of ourNames will be string.
What is the type of count in:
let count;
count = 10;
Any
What is the type of result in:
function addTwo(a) { return a + 2; } const result = addTwo(4);
any
What is the type of result in:
function addTwo(a =1) { return a + 2; } const result = addTwo(4);
number
What is the type of result in:
let result;
any
Will this yield any error?
If not what will be the type of result?
let result; result = "Math"; result = 18; result = true; result = new Date(2023, 1, 1);
No error. The type is any
What does the type:
{ [field: string]: any }
means ?
It’s an object whose property names are of type string, with property values of type any.
What kind of type checking those Typescript do on the any type?
None
What is the type void?
The void type represents a function’s return type when it doesn’t return any data.