Type System & Custom Types Flashcards
Interfaces vs Types Enums Literal Types Union & Intersection Types Type Aliases Tuples (47 cards)
What is an interface in TypeScript?
An interface defines a contract for object shapes, describing property names and their types.
What is a type alias in TypeScript?
A type alias creates a new name for a type using the ‘type’ keyword.
What is the difference between interface and type in TypeScript?
Interfaces are best for object shapes and can be extended or merged; types are more flexible and can represent primitives, unions, tuples, and more.
When should you prefer an interface over a type?
Prefer interfaces when defining object shapes or class contracts, as they support declaration merging and extension.
When should you prefer a type over an interface?
Prefer type when using unions, intersections, or working with primitives and complex type compositions.
What is an enum in TypeScript?
An enum is a special TypeScript feature for defining named constants that can be numeric or string-based.
What are the advantages of enums?
Enums improve code readability and reduce magic numbers or strings in the codebase.
What are the disadvantages of enums?
Enums increase bundle size and add runtime code, unlike union literal types which are more lightweight.
What is a literal type in TypeScript?
A literal type restricts a variable to have a specific value, like ‘on’ | ‘off’.
What is a union type in TypeScript?
A union type allows a variable to hold one of several specified types, e.g., string | number.
What is an intersection type in TypeScript?
An intersection type combines multiple types into one, requiring all properties of each to be present.
What is a tuple in TypeScript?
A tuple is a fixed-length array with specified types for each position, e.g., [string, number].
What is a use case for tuples?
Use tuples when working with pairs or structured but fixed-length heterogeneous data, like coordinates or key-value pairs.
What is a potential gotcha with enums?
Unlike other types, enums exist at runtime, so they can unexpectedly increase the size of transpiled JavaScript.
Can interfaces extend other interfaces?
Yes, interfaces can extend one or multiple other interfaces using the ‘extends’ keyword.
What is a best practice for using union types?
Use union types for constrained input values or status flags to increase code safety.
What is a real-world tradeoff between enums and literal unions?
Enums add runtime overhead, while literal types offer better tree-shaking and are more performant in front-end apps.
What’s the impact of type aliases on system design?
They improve abstraction, reduce duplication, and make the codebase easier to maintain.
What are the architectural implications of using interfaces extensively?
Interfaces can enforce contracts across layers and improve modularity and separation of concerns.
What’s a performance benefit of literal union types over enums?
Literal types are erased during compilation and do not exist at runtime, reducing output size.
What is a common interview question about interfaces vs types?
What are the differences between type aliases and interfaces in TypeScript and when would you use one over the other?
What’s a potential issue when using complex intersection types?
Deeply nested or conflicting property types can make intersections hard to debug and maintain.
How can union types help with fault tolerance?
Union types constrain input values, reducing invalid states and runtime errors.
What is a debugging benefit of using literal types?
Literal types provide clearer compiler errors and auto-completions for restricted values.