Fundamentals Flashcards

1
Q

What is TypeScript?

A

TypeScript is a superset of JavaScript, In other words, all Javascript programs are already TypeScript programs. TypeScript has some syntax of its own, so TypeScript programs are not, in general, valid JavaScript programs.

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

What does TypeScript compile to?

A

JavaScript

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

What is a type system?

A

A type system is a set of rules that a typechecker uses to assign types to your
program.

There are generally two kinds of type systems: type systems in which you have to tell
the compiler what type everything is with explicit syntax, and type systems that infer
the types of things for you automatically.

TypeScript is inspired by both kinds of type systems: you can explicitly annotate your
types, or you can let TypeScript infer most of them for you.

Boris Cherny, Programming TypeScript - Making Your JavaScript Applications Scale (O’Reilly 2019), p. 17

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

What is type inference?

A

TypeScript knows the JavaScript language and will generate types for you in many cases.

For example in creating a variable and assigning it to a particular value, TypeScript will use the value as its type.

let helloWorld = "Hello World"; // <== Infered type is `string`
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when TypeScript can not infer a type?

A

It will assign the type any.

This is dangerous because variables with type any are excluded from any kind of type checking and this can in some cases extend to the logic that uses such variables.

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

How can you annotate the type of a variable?

A

With : TypeName

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

What is a JS SyntaxError?

A

The SyntaxError object represents an error when trying to interpret syntactically invalid code. It is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.

Source MDN

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

What is a JS TypeError?

A

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.

Source MDN

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

When does JS throw SyntaxError and TypeError errors?

A

At runtime and at compile time

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

When does TS throw the equivalent to JS SyntaxError and TypeError errors?

A

TypeScript throws both syntax-related errors and type-related errors at compile time.

This type of errors are also known as non-exception failures.

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

What is a Structural Type System?

A

One of TypeScript’s core principles is that type checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural typing”.

In a structural type system, if two objects have the same shape, they are considered to be of the same type.

interface Point {
  x: number;
  y: number;
}
 
function logPoint(p: Point) {
  console.log(`${p.x}, ${p.y}`);
}
 
// logs "12, 26"
const point = { x: 12, y: 26 };
logPoint(point);

The point variable is never declared to be a Point type. However, TypeScript compares the shape of point to the shape of Point in the type-check. They have the same shape, so the code passes.

The shape-matching only requires a subset of the object’s fields to match.

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

What are type annotations?

A

Type annotations in TypeScript are lightweight ways to record the intended contract of the function or variable.

The syntax for a type annotation is to put a colon (:) followed by the type after the variable name before any assignment.

Example:

function greeter(person: string) {
  return "Hello, " + person;
}
 
let user: number[] = [0, 1, 2];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Type inference?

A

In TypeScript, there are several places where type inference is used to provide type information when there is no explicit type annotation. For example, in this code

let x = 3; // Type is number

The type of the x variable is inferred to be number. This kind of inference takes place when initializing variables and members, setting parameter default values, and determining function return types.

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