TypeScript Flashcards
(49 cards)
What does TypeScript add to JavaScript?
Static type checking; TypeScript programs are type-checked and compiled to javascript before running.
How is TypeScript executed?
After type checking, it’s compiled to JavaScript which runs in any JS environment (e.g. it can be run in browsers).
Why prefer TypeScript over JavaScript?
TypeScript addresses JavaScript’s eccentricities and adds static typing and class-based OOP.
Who created JavaScript and how long did it take?
Written in 10 days in 1995 by one engineer at Netscape.
Why was JavaScript created?
To make web pages interactive, similar in feel to Java.
How has JS evolved since its creation?
From small snippets to massive codebases; added structured constructs and classes.
What does TypeScript’s static type checking mean?
Types are known at compile time, preventing type errors before runtime.
What is the ‘any’ type in TypeScript?
Catch-all type; simulates disabling type checking; should be used sparingly.
You can add an any time to another any time
What is the ‘unknown’ type in TypeScript?
Safer than ‘any’; must verify type before operations, of which there are a limited set
- nothing is inferred as unknown, it must be assigned.
What are boolean literal types?
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.
What are literal types in TypeScript?
Specify exact values a variable must have.
What is the bigint type for?
Working with very large integers.
let f: bigint = 100n
What is the symbol type?
what are the two ways to declare symbol types?
Represents unique and immutable values.
let a = Symbol(‘a’)
let b: symbol = Symbol(‘b’)
What is an object type in TypeScript?
Show how to declare an object type:
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.
What are optional and readonly properties?
Optional: may or may not be present; Readonly: properties that cannot be reassigned after initialisation.
What are type aliases in typescript?
You can create new names for types using type aliases.
What can the union and intersection symbols be used for in typescript?
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).
What type is T[] in TypeScript?
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[]
What happens when you create an untyped empty array?
What happens when you create an untyped array with an element in?
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 are tuples declared?
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]
How to define optional and rest elements in tuples?
? for optional; … for rest elements.
let list: [number, boolean, …string[]] = [1, false, ‘a’, ‘p’]
How to type function parameters in TypeScript?
Specify types inline, can also include optional/default values.
What are rest parameters in TypeScript?
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.
What does the reduce function do?
Traverses the array and accumulates a result, e.g., summing all values.