Give an example of a TS boolean assignment statement.
let isDone: boolean = false;
In TS, all numbers are ____
… floating point values.
Give 4 examples of number assignments, each using a different literal type.
let decimalNum: number = 7;
let hexNum: number = 0xf00d;
let binaryNum: number = 0b1010;
let octalNum: number = 0o744;
Give an example of a string assignment.
let description: string = 'A great text for all ages';
What are the two ways to define an array of number values [1, 2, 3]?
let list: number[] = [1, 2, 3]
let list: Array<number> = [1, 2, 3]</number>
What are tuples and give an example?
Tuples allow you to define a fixed number of elements in an array, where the values may be of varrying types.
let greeting: [string, number] = ["Hello", 24601]
When accessing an element outside of the known indices in a tuple, a ___ type is used instead
union
What do enums do?
They give a more friendly name to sets of numeric values.
Describe enum numbering
00, even if you set a later one to 0Create an enum of the 3 primary colors.
Access the numeric value of one of them.
Also access the name value of the second member and what does it print?
enum Color {Red = 2, Green, Blue}
Color.Green
Color[3] - prints Green
What does the any type allow?
It allows us to opt-out of type checking during compile-time checks for values that may be coming from dynamic content or 3rd party libraries, or we might for some other reason, not know its type.
Why doesn’t the following work?
`let aNum: Object = 4
aNum.toFixed()`
toFixed is a method from Number.prototype.
That method does not exist on the base Object JavaScript class, which is what we defined our variable to be.
Give a handy trick that you can use any for.
If you know part of the type but not all of it. Such as, you know it’s an array but you don’t know the types that will fill it up.
let userData: any[] = ['hey', 24601]
let userData: Array<any> = ['hey', 24601]</any>
What is void for, and when do you commonly see it?
What’s one more notable thing about in in relation to variables?
void is the absense of having any type at all, commonly seen as the return type of functions that do not return a value.
`function warnUser(): void {
console.log(‘Warning’)
}`
It’s not useful to declare variables of type void because only undefined and null can be then assigned to the variable.
Describe undefined and null.
undefined and null.let u: undefined = undefinedundefined to a number type.--strictNullCheck flag, undefined and null can only be assigned to void and their respective typesnull, better to use an explicity union type:let userNum: string | nullWhat does the type never represent?
never is the return type of a function that always throws an error or one that never completesnever if it’s found to never be able to completeGive an example of a function with a never type that does not error out.
`function keepDoing(): never {
while(true) {}
}`
Describe the object type
object is a type that describes the non-primitive type.number, string, boolean, symbol, null, or undefinedWhat is a type assertion?
A type assertian is a well to tell the compiler that you know more about a variable’s type than it does. This usually happens when you know that it’s more specific than it’s currently described as.
Give two ways to represent a type assertion?
What’s special about one of them?
`let someValue: any = “I’m a cool string”
let lengthVal: number = (<string>someValue).length`</string>
`let someValue: any = “I’m a cool string again”
let lengthVal: number = (someValue as string).length`
This first method is not permitted in JSX. You must use the as syntax in JSX.