Concepts Flashcards
Is Typescript’s typing Nominal or Structural? Describe why.
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!
What is excess property checking and what impact does it have for object literals?
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.
What is the convention used to format Type Alias names?
TitleCase is the common convention
When would you use a Type Alias over an Interface?
You would use a Type Alias when defining something other than an object (a single value for example) such as a Union Operator (|).
When would you use an Interface over a Type Alias?
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.
Describe the bahaviour of Union Types
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.
Describe the behaviour of Intersection Types
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.
What is your understanding of Excess Property Checking?
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.
What are Const Assertions and why might you use them?
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
.
What does the keyof
operator do?
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.
What are common uses of the keyof
operator?
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.
What does the typeof
operator do and why might you use it?
Thetypeof
type 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.
What is the convention used for naming Interfaces?
TitleCase (PasclaCase) should be used.
What are attributes of Interfaces that differentiate them for Type Aliases?
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
Describe at a basic level how the Type Registry pattern works in Typescript.
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
.
Why is it important to type the return value as void
from functions which don’t return anything?
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.