Concepts Flashcards

1
Q

Is Typescript’s typing Nominal or Structural? Describe why.

A

Typescript is a structurally typed.
Typescript doesn’t care about the name/constructor from which data came from… all it cares about is the structure of the data being assessed!

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

What is excess property checking and what impact does it have for object literals?

A

Excess property checking is apparent when you attempt to pass unused parameters into a function with typed parameters. Typescript is aware that the parameter will never be used and warns you as such.

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

What is the convention used to format Type Alias names?

A

TitleCase is the common convention

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

When would you use a Type Alias over an Interface?

A

You would use a Type Alias when defining something other than an object (a single value for example) such as a Union Operator (|).

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

When would you use an Interface over a Type Alias?

A

One reason to use an Interface is that Interfaces are open. By using an Interface you give yourself (or others) the flexibility to modify or augment the interface further downstream.

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

Describe the bahaviour of Union Types

A

Union Types (|) can be thought of as OR join. When using a union type to combine two different types, we are saying that the resulting type should accept the first type OR the second type.

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

Describe the behaviour of Intersection Types

A

Intersection types (&) can be thought of as AND joins. When using a Intersection type with two different types, we are saying that the resulting type should accept only the shared elements between the first type AND the second type.

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

What is your understanding of Excess Property Checking?

A

Typescript will show an error if you define an excess property on an object which it knows that will never be used.

For example, if you attempt to pass an additional parameter into a function which does not exist in the type defined for that function.

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

What are Const Assertions and why might you use them?

A

Const Assertions are when you use the as const syntax against a value.

Doing so prevents the widening of object and array properties/values, instead remaining literals, becoming readonly.

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

What does the keyof operator do?

A

The keyof operator takes an object type and produces a string or numeric literal union of its keys.

Remember that keyof works with types, not with values.

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

What are common uses of the keyof operator?

A

The keyof operator is commonly used to:

  • Type-safe Property Access: Create a union type of all the keys in an object type. This allows you to ensure that when accessing properties of an object, you’re using valid keys.
  • Generic Constraints: Constrain generic types to ensure they are valid keys of another type, adding more type safety.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the typeof operator do and why might you use it?

A

Thetypeoftype query allows you to extract a type from a value.

The typeof operator is commonly used to:
- Type Inference: Extract the type of a variable or constant to reuse it elsewhere in your code.
- Avoiding Redundant Type Definitions: Simplify types by directly referencing the type of an existing variable.

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

What is the convention used for naming Interfaces?

A

TitleCase (PasclaCase) should be used.

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

What are attributes of Interfaces that differentiate them for Type Aliases?

A

With Interfaces, you can:
- extend
- Can ask a class to produce instances that conform to a given interface through implements
- Are open! Can have multiple declarations in the same scope which get merged. Open interfaces

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

Describe at a basic level how the Type Registry pattern works in Typescript.

A

At a very basic level: The use of Open Interfaces and the ability to use declare module to populate the Interface from other locations in code. You create the registry with the open interface, and allow it to be populated further down the line through the use of declare module.

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

Why is it important to type the return value as void from functions which don’t return anything?

A

Using undefined might be troublesome as many functions return a value, even if not used. By using void we say we don’t care what value is being returned. With the latter it’s important that you don’t return something specifically… you’ll get yelled at anyway.