Type System & Custom Types Flashcards

Interfaces vs Types Enums Literal Types Union & Intersection Types Type Aliases Tuples (47 cards)

1
Q

What is an interface in TypeScript?

A

An interface defines a contract for object shapes, describing property names and their types.

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

What is a type alias in TypeScript?

A

A type alias creates a new name for a type using the ‘type’ keyword.

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

What is the difference between interface and type in TypeScript?

A

Interfaces are best for object shapes and can be extended or merged; types are more flexible and can represent primitives, unions, tuples, and more.

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

When should you prefer an interface over a type?

A

Prefer interfaces when defining object shapes or class contracts, as they support declaration merging and extension.

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

When should you prefer a type over an interface?

A

Prefer type when using unions, intersections, or working with primitives and complex type compositions.

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

What is an enum in TypeScript?

A

An enum is a special TypeScript feature for defining named constants that can be numeric or string-based.

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

What are the advantages of enums?

A

Enums improve code readability and reduce magic numbers or strings in the codebase.

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

What are the disadvantages of enums?

A

Enums increase bundle size and add runtime code, unlike union literal types which are more lightweight.

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

What is a literal type in TypeScript?

A

A literal type restricts a variable to have a specific value, like ‘on’ | ‘off’.

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

What is a union type in TypeScript?

A

A union type allows a variable to hold one of several specified types, e.g., string | number.

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

What is an intersection type in TypeScript?

A

An intersection type combines multiple types into one, requiring all properties of each to be present.

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

What is a tuple in TypeScript?

A

A tuple is a fixed-length array with specified types for each position, e.g., [string, number].

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

What is a use case for tuples?

A

Use tuples when working with pairs or structured but fixed-length heterogeneous data, like coordinates or key-value pairs.

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

What is a potential gotcha with enums?

A

Unlike other types, enums exist at runtime, so they can unexpectedly increase the size of transpiled JavaScript.

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

Can interfaces extend other interfaces?

A

Yes, interfaces can extend one or multiple other interfaces using the ‘extends’ keyword.

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

What is a best practice for using union types?

A

Use union types for constrained input values or status flags to increase code safety.

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

What is a real-world tradeoff between enums and literal unions?

A

Enums add runtime overhead, while literal types offer better tree-shaking and are more performant in front-end apps.

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

What’s the impact of type aliases on system design?

A

They improve abstraction, reduce duplication, and make the codebase easier to maintain.

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

What are the architectural implications of using interfaces extensively?

A

Interfaces can enforce contracts across layers and improve modularity and separation of concerns.

20
Q

What’s a performance benefit of literal union types over enums?

A

Literal types are erased during compilation and do not exist at runtime, reducing output size.

21
Q

What is a common interview question about interfaces vs types?

A

What are the differences between type aliases and interfaces in TypeScript and when would you use one over the other?

22
Q

What’s a potential issue when using complex intersection types?

A

Deeply nested or conflicting property types can make intersections hard to debug and maintain.

23
Q

How can union types help with fault tolerance?

A

Union types constrain input values, reducing invalid states and runtime errors.

24
Q

What is a debugging benefit of using literal types?

A

Literal types provide clearer compiler errors and auto-completions for restricted values.

25
What is a potential gotcha when using tuples?
Accessing tuple elements out of bounds or misunderstanding their fixed structure can lead to bugs.
26
What is declaration merging?
Declaration merging allows multiple declarations of the same interface to be combined automatically.
27
What’s a best practice for extending types or interfaces?
Favor composition over duplication and use 'extends' for shared contracts to promote reuse.
28
What’s a disadvantage of intersection types?
Complexity increases with deeply nested or overlapping properties, which can reduce readability.
29
Can type aliases be recursive?
Yes, type aliases can reference themselves recursively for complex type structures like trees or JSON.
30
What’s a common use case for string literal types?
Defining allowed values for input fields, configuration flags, or component props.
31
What’s the syntax to define a type alias for a union of string values?
type Status = 'active' | 'inactive' | 'pending';
32
What is the syntax to define a tuple with a string and number?
let pair: [string, number] = ['hello', 42];
33
Can tuples have optional elements?
Yes, tuples can have optional elements using ? after the type: [string, number?]
34
What’s the advantage of interfaces in large codebases?
They help enforce contracts and make it easier to reason about shapes of objects across modules.
35
What’s the impact of using enums in server-side TypeScript apps?
Their runtime availability can be beneficial for reflection and validation but may increase bundle size.
36
Can interfaces describe function types?
Yes, interfaces can be used to describe function signatures using call signatures.
37
What’s the difference between const enums and regular enums?
const enums are inlined at compile time and do not generate runtime code.
38
What’s a common interview question about union vs intersection types?
Explain the difference between union and intersection types with examples and when to use each.
39
What is an example of an intersection type?
type A = { a: string }; type B = { b: number }; type AB = A & B;
40
What’s a good way to organize type aliases and interfaces in a project?
Group related types in separate files or folders and use barrel exports for easy imports.
41
What is the downside of excessive type composition?
Over-composing types can lead to poor readability and hard-to-debug compiler errors.
42
What is the main benefit of using type aliases for primitives?
Improves code readability and semantic meaning, e.g., type UserID = string;
43
What does TypeScript do with types at runtime?
Types are erased at compile time and do not exist at runtime; they are purely for static checking.
44
What’s a best practice for naming types and interfaces?
Use PascalCase and descriptive names like UserResponse or IConfigOptions.
45
What is a benefit of using tuples over arrays?
Tuples provide predictable positions and types, which increases type safety for fixed-structure data.
46
How do literal types improve debugging?
They produce clear error messages when a value doesn't match one of the expected literals.
47
Can interfaces be implemented by classes?
Yes, interfaces define contracts that classes can implement using the 'implements' keyword.