TypeScript Flashcards

(49 cards)

1
Q

What does TypeScript add to JavaScript?

A

Static type checking; TypeScript programs are type-checked and compiled to javascript before running.

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

How is TypeScript executed?

A

After type checking, it’s compiled to JavaScript which runs in any JS environment (e.g. it can be run in browsers).

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

Why prefer TypeScript over JavaScript?

A

TypeScript addresses JavaScript’s eccentricities and adds static typing and class-based OOP.

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

Who created JavaScript and how long did it take?

A

Written in 10 days in 1995 by one engineer at Netscape.

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

Why was JavaScript created?

A

To make web pages interactive, similar in feel to Java.

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

How has JS evolved since its creation?

A

From small snippets to massive codebases; added structured constructs and classes.

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

What does TypeScript’s static type checking mean?

A

Types are known at compile time, preventing type errors before runtime.

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

What is the ‘any’ type in TypeScript?

A

Catch-all type; simulates disabling type checking; should be used sparingly.

You can add an any time to another any time

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

What is the ‘unknown’ type in TypeScript?

A

Safer than ‘any’; must verify type before operations, of which there are a limited set
- nothing is inferred as unknown, it must be assigned.

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

What are boolean literal types?

A

They assign an exact value to a variable - the value it must have.
In the code- let e: true = true, e can only ever be assigned true.

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

What are literal types in TypeScript?

A

Specify exact values a variable must have.

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

What is the bigint type for?

A

Working with very large integers.

let f: bigint = 100n

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

What is the symbol type?

what are the two ways to declare symbol types?

A

Represents unique and immutable values.

let a = Symbol(‘a’)
let b: symbol = Symbol(‘b’)

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

What is an object type in TypeScript?

Show how to declare an object type:

A

Defines the shape of an object and enforces it.

let a: object = { b: string } //this means an object called a with the property b which is a string.

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

What are optional and readonly properties?

A

Optional: may or may not be present; Readonly: properties that cannot be reassigned after initialisation.

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

What are type aliases in typescript?

A

You can create new names for types using type aliases.

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

What can the union and intersection symbols be used for in typescript?

A

You can create new types by combining existing types the using | (union)
and & (intersection) operators.

type CatorDog = Cat | Dog creates a new type that can be either Cat or Dog (or both).

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

What type is T[] in TypeScript?

A

An array of type T elements.

As it’s not annotated a = [1,2,3] will be inferred to have type number[] and b = [‘a’, ‘b’] will be inferred to have type string[]

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

What happens when you create an untyped empty array?

What happens when you create an untyped array with an element in?

A

TypeScript infers it as type ‘any’.

TypeScript infers the type from the initial elements, e.g. let f = [‘red’] is inferred to be of type string[]

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

How are tuples declared?

A

Fixed-length arrays with specified types for each element. Trying to assign a tuple of incorrect type results in a compile error.

e.g. let b : [string, string, number] = [‘a’, ‘f’, 4]

21
Q

How to define optional and rest elements in tuples?

A

? for optional; … for rest elements.

let list: [number, boolean, …string[]] = [1, false, ‘a’, ‘p’]

22
Q

How to type function parameters in TypeScript?

A

Specify types inline, can also include optional/default values.

23
Q

What are rest parameters in TypeScript?

A

Allow functions to accept any number of arguments as an array.
function sumNums(…numbers: number[]): number {
return numbers.reduce(total, n) => total + n, 0}

The …numbers parameter is a rest parameter. It represents any number of number arguments.

24
Q

What does the reduce function do?

A

Traverses the array and accumulates a result, e.g., summing all values.

25
What function methods does TypeScript support?
apply, call, and bind function add(a: number, b:number): number { return a + b } add.apply(null, [1. 2]) add.call(null, 1, 2) add.bind(null, 1, 2) // all evaluate to 3.
26
How do you create type aliases for function types?
Use the 'type' keyword to define a function signature. type Log = (message: string, userId?: string) => void
27
What are generic types in TypeScript?
Types with free type parameters, allowing flexibility with different data types. type Filter = {(array: T[]. f: (item: T) => boolean): T[]} T is a type parameter, a placeholder for the actual type that will be passed to the Filter type later.
28
What does the Filter function do?
Filters an array based on a predicate function that returns true/false.
29
How does Filter use generics?
Uses a type parameter T as a placeholder for the actual type.
30
What does Filter1 vs. Filter2 illustrate?
Difference in when type T is instantiated: call-time for Filter1, definition-time for Filter2.
31
What is the Map generic function?
Takes an array of T and a function that converts T to U, returns an array of U. function map (array: T[], f: (item: T) => U): U[] { let result = [] for (let i = 0; i < array.length; i++) { result[i] = f(array[i]} } return result }
32
How are classes and inheritence defined in Typescript.
class Piece { type Colour = "Black" } class King extends Piece {}
33
What are TypeScript's access modifiers?
private, protected, and public control access levels of class members. - private modifier allows access within the same class. - protected modifier allows access within the same class and subclasses. - public modifier allows access from any location.
34
What are abstract classes in TypeScript?
Classes that cannot be instantiated directly and can have abstract methods, which are accessible to and implemented by concrete classes.
35
What is the purpose of abstract methods?
Defined in abstract classes, they must be implemented by concrete subclasses.
36
What are static methods?
Methods tied to the class, not instances; can only access other static members.
37
What is structural typing?
Objects with the same structure are considered the same type. They can share methods per say. -In other languages if you define a function for zebras if you try use it on poodles, it won’t work because the type checker assumes that if you wanted to use the zebra trot method on the poodle you would’ve made it a zebra. -TypeScript is more relaxed: If we define a function that takes Zebra, it can also accept a Poodle, because structurally the two classes are similar.
38
How do generics apply to classes?
You can define generic type parameters and use them throughout the class. Later on when type checking is run, the types will be instantiated.
39
What is an interface in TypeScript?
Another way to define the types of objects. type Sushi = { calories: number tasty: boolean } is equivalent to: interface Sushi = { calories: number tasty: boolean }
40
What are intersection types?
Combine multiple types into one using '&'. Used for inheritance for classes in typescript: Type Sushi = Food & {salt: boolean}
41
How does interface inheritance work?
An interface can extend another, inheriting and adding properties. interface sushi extends Food {salty: boolean}
42
What is declaration merging?
TypeScript automatically combines multiple declarations with the same name. interface User { age: number} interface User { name: string } let a: User = { age: 24, name: anya}
43
Can classes implement interfaces?
Yes, and they can implement multiple interfaces. class Cat implements Animal, Feline { eat(food: string) { console.info('ate food')} }
44
What happens if you don't include types in your typescript?
They can be inferred at compile time
45
Question: what’s the difference between TS type inference and JS type inference?
Typescript inference happens at compile time whereas with JavaScript inference happens at runtime.
46
What is the syntax for assigning the text hello world to a variable and then printing it?
let message: string = 'Hello, World!'; console.log(message);
47
What command is used to compile the typescript file to a JavaScript file called hello_world, using the typescript compiler? What is the command for running this file using node.js?
tsc hello_world.ts - this creates a hello_world.js file node hello_world.js
48
Describe advanced object types?
Advanced object types can have optional properties, numeric property names and index signatures - c?: string // means c is an optional property of type string - [key: number] is an index signature, so any number of numeric keys are allowed
49
What are the 4 methods of typing function parameters:
function greet(name: string) {return 'hello ' + name} //named function let greet2 = function(name, string) {return 'hello ' + name} //function expression let greet 3 = (name: string) => {return 'hello ' + name} //arrow function expression let greet 4 = (name: string) => 'hello ' + name //shorthand arrow function expression