Typescript 1 Flashcards
What is a statically typed language?
It’s type-checked and compiled before you can run it.
In Typescript, can you change the type of a variable partway through a program?
No
What does Typescript need to be compiled into before it’s run in a browser?
TypeScript needs to be compiled into JavaScript before it can be run in a browser.
When do you receive an error if your code is not correctly typed in Typescript?
At compile time.
What are the 2 ways you can declare a type in Typescript?
Explicit type annotations:
let a: number = 1 // a is a number
let b: string = ‘hello’ // b is a string
let c: boolean[] = [true, false] // c is an array of
Inferred types:
let a = 1 // a is a number
let b = ‘hello’ // b is a string
let c = [true, false] // c is an array of Booleans
What is the command for the Typescript compiler?
$> tsc hello_world.ts
What is the Any type?
The any type is a catch-all type:
let a: any = 123
let b: any = [‘danger’]
let c = a + b // Value is ‘123danger’
Use any sparingly. Overuse can remove the benefits of type-checking
What is the Unknown type?
The unknown type is a safer counterpart to any.
TypeScript will never infer something as unknown. You have to explicitly annotate it:
let a: unknown = 30;
It only supports a limited set of operations. You can just compare
unknown values (using ==, ===, !=, !==)
What is a Literal type?
Literal types allow you to specify the exact value a variable must have.
let e: true = true, e can only ever be true.
let f: 26.218 = 26.218
What is a symbol type?
TypeScript has a symbol type which is unique and immutable data type and is often used to identify object properties.
let a = Symbol('a') // symbol
How do you define an object in Typescript?
let a: object = {
b: ‘x’
}
console.log(a[‘b’])
let c: { d : string } = {
d: ‘y’
}
console.log(c.d)
The type {d: string} means an object with a string property d
How are object types enforced in Typescript?
TypeScript enforces the object shape defined by the type.
let a: {b: number}
a = {} // Error TS2741: Property ‘b’ is missing in type ‘{}’
// but required in type ‘{b: number}’.
a = {
b: 1,
c: 2 // Error TS2322: Type ‘{b: number; c: number}’ is not
} // assignable to type ‘{b: number}’
How to define an optional value in Typescript?
c? means c is an optional property.
let a: {
b: number
c?: string
[key: number]: boolean
}
How to define a read only property in Typescript?
TypeScript allows properties to be marked as ‘readonly‘ which means
they can’t be reassigned after initialisation.
let user: {
readonly firstName: string
} = {
firstName: ‘abby’
}
How to declare Type Aliases in Typescript?
You can create new names for types using type aliases:
type Age = number
type Person = {
name: string
age: Age
}
let age: Age = 55
let driver: Person = {
name: ‘James May’,
age: age
}
How do you use a union operator in Typescript?
type CatOrDog = Cat | Dog
Cat | Dog creates a new type that can be either Cat or Dog (or both)
How do you use an intersection operator in Typescript?
type CatAndDog = Cat & Dog
Cat & Dog creates a new type that combines Cat and Dog.
How do you declare a tuple in Typescript?
// A tuple of [first name, last name, birth year]
let b : [string, string, number]
= [‘Malcolm’, ‘Gladwell’, 1963]
Can you have optionals in a tuple in Typescript?
You can specify optional elements in a tuple type using the ? operator.
// An array of train fares, which sometimes vary depending on direction
let trainFares: [number, number?][] = [
[3.75],
[8.25, 7.70],
[10.50]
]
What is a rest element in a tuple in Typescript?
TypeScript allows you to specify rest elements in a tuple type using the … operator.
// A list of strings with at least 1 element
let friends: [string, …string[]]
= [‘Sara’, ‘Tali’, ‘Chloe’, ‘Claire’]
In friends, the rest elements are of type string[].
This means friends must have at least one element, and the rest of the elements (if any) must be strings.
What do apply call and bind do in Typescript?
TypeScript supports the apply, call, and bind methods on functions:
function add(a: number, b: number): number {
return a + b
}
add(10, 20) // evaluates to 30
add.apply(null, [10, 20]) // evaluates to 30
add.call(null, 10, 20) // evaluates to 30
add.bind(null, 10, 20)() // evaluates to 30
Apply and call invoke the function whereas bind modifies the function
How do you create a generic type in Typescript?
TypeScript allows you to create generic types.
type Filter = {
<T>(array: T[], f: (item: T) => boolean): T[]
}
let myFilter: Filter = (array, f) => array.filter(f)
Here, T is a type parameter. It’s a placeholder for the actual type that will be passed to the Filter type.
What is the difference between these filters?
type Filter1 = {
<T>(array: T[], f: (item: T) => boolean): T[]
}
let filter1: Filter1 = (array, f) => [ /*...*/]
type Filter2<T> = {
(array: T[], f: (item: T) => boolean): T[]
}
let filter2: Filter2<number> = (array, f) => [ /*...*/]
Notice the difference between the definitions of Filter1 and Filter2:
In Filter1, the <T> is on the right of the assignment operator
In Filter2, the <T> is on the left of the assignment operator</T></T>
This difference has the consequence that the type T is bound at different
times for Filter1 and Filter2:
Filter1 usage:
Type T is instantiated at call time, based on the arguments passed to filter1:
let result1 = filter1([1, 2, 3], x => x > 2);
let result2 = filter1([“a”, “b”, “c”], x => x !== “b”);
Filter2 usage:
Type T is fixed when Filter2 is defined (i.e. filter2: Filter2<number>):
let result3 = filter2([1, 2, 3], x => x > 2);
let result4 = filter1(["a", "b", "c"], x => x !== "b"); //Error</number>