Handbook: Basic Types Flashcards

1
Q

Give an example of a TS boolean assignment statement.

A

let isDone: boolean = false;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In TS, all numbers are ____

A

… floating point values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give 4 examples of number assignments, each using a different literal type.

A

let decimalNum: number = 7;

let hexNum: number = 0xf00d;

let binaryNum: number = 0b1010;

let octalNum: number = 0o744;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give an example of a string assignment.

A

let description: string = 'A great text for all ages';

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the two ways to define an array of number values [1, 2, 3]?

A

let list: number[] = [1, 2, 3]

let list: Array<number> = [1, 2, 3]</number>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are tuples and give an example?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When accessing an element outside of the known indices in a tuple, a ___ type is used instead

A

union

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do enums do?

A

They give a more friendly name to sets of numeric values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe enum numbering

A
  • Enums begin numbering their members at 0
  • You can manually set the values of members
  • If you don’t set the first element, it will always start at 0, even if you set a later one to 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create 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?

A

enum Color {Red = 2, Green, Blue}

Color.Green

Color[3] - prints Green

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the any type allow?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why doesn’t the following work?

`let aNum: Object = 4

aNum.toFixed()`

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give a handy trick that you can use any for.

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is void for, and when do you commonly see it?

What’s one more notable thing about in in relation to variables?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe undefined and null.

A
  • They each of their own type, respectively undefined and null.
    • let u: undefined = undefined
  • Both types are, by default, subtypes of all other types. This means that you can assign undefined to a number type.
    • Using the --strictNullCheck flag, undefined and null can only be assigned to void and their respective types
  • If wanted a string to optionally be an actual string literal or null, better to use an explicity union type:
    • let userNum: string | null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the type never represent?

A
  • Represents the types of values that never occur.
  • For example, never is the return type of a function that always throws an error or one that never completes
    • A function can have an inferred type of never if it’s found to never be able to complete
17
Q

Give an example of a function with a never type that does not error out.

A

`function keepDoing(): never {

while(true) {}

}`

18
Q

Describe the object type

A
  • object is a type that describes the non-primitive type.
    • anything that is not number, string, boolean, symbol, null, or undefined
19
Q

What is a type assertion?

A

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.

20
Q

Give two ways to represent a type assertion?

What’s special about one of them?

A

`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.