Handbook: Basic Types Flashcards
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
- 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 to0
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?
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
.
- They each of their own type, respectively
undefined
andnull
.let u: undefined = undefined
- Both types are, by default, subtypes of all other types. This means that you can assign
undefined
to anumber
type.- Using the
--strictNullCheck
flag,undefined
andnull
can only be assigned tovoid
and their respective types
- Using the
- If wanted a string to optionally be an actual string literal or
null
, better to use an explicity union type:let userNum: string | null