Functions & Generics Flashcards
Function Signatures Optional and Default Parameters Rest Parameters Generic Functions Constraints on Generics (47 cards)
What is a function signature in TypeScript?
A function signature defines the parameter types and return type of a function.
How do you define a function signature in TypeScript?
You specify parameter types and return type: (x: number, y: number) => number.
What is the benefit of function signatures?
They improve type safety, code readability, and IDE support.
What are optional parameters in TypeScript?
Parameters that may be omitted when calling a function, marked with a ?
.
What are default parameters in TypeScript?
Parameters that assume a default value if not explicitly passed.
What is a rest parameter in TypeScript?
It collects all remaining arguments into an array using the ...
syntax.
Can rest parameters and optional parameters be used together?
Yes, but rest parameters must come last in the parameter list.
What is a generic function in TypeScript?
A function that works with different types using a placeholder like <T>.</T>
What is a constraint on generics in TypeScript?
It restricts the allowed types that a generic can accept using extends
.
Why use generics in functions?
To write reusable, type-safe functions that work across multiple types.
What’s an example of a generic function?
function identity<T>(arg: T): T { return arg; }</T>
How do you constrain a generic to accept only objects with a specific property?
Use T extends { length: number }
to require a length
property.
What are advantages of optional parameters?
Improve flexibility and make function calls simpler when not all data is available.
What are disadvantages of optional parameters?
May lead to ambiguous function behavior if overused.
What are advantages of default parameters?
Avoids undefined values and provides fallback behavior without checking manually.
What are potential issues with rest parameters?
They can lead to unexpected results if argument order is misunderstood.
What’s a use case for rest parameters?
Variadic functions like sum(...args: number[])
or logging utilities.
What’s a use case for generic functions?
Functions like map
, filter
, or utility methods that should work with any type.
What’s a use case for generic constraints?
Validating properties in objects, like ensuring the presence of an ID or key.
What is the impact of generics on system design?
They allow for highly abstract and reusable code components, reducing duplication.
What is the architectural implication of generic functions?
Encourages writing DRY utility functions and shared logic modules.
What is a performance implication of using generics?
None at runtime—generics are erased during compilation and don’t affect performance.
How do generics affect debugging?
They provide better type safety at compile time, but don’t exist in runtime logs or stack traces.
What’s a tradeoff of overusing generics?
Too much abstraction can hurt readability and make the function harder to understand.