Functions & Generics Flashcards

Function Signatures Optional and Default Parameters Rest Parameters Generic Functions Constraints on Generics (47 cards)

1
Q

What is a function signature in TypeScript?

A

A function signature defines the parameter types and return type of a function.

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

How do you define a function signature in TypeScript?

A

You specify parameter types and return type: (x: number, y: number) => number.

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

What is the benefit of function signatures?

A

They improve type safety, code readability, and IDE support.

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

What are optional parameters in TypeScript?

A

Parameters that may be omitted when calling a function, marked with a ?.

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

What are default parameters in TypeScript?

A

Parameters that assume a default value if not explicitly passed.

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

What is a rest parameter in TypeScript?

A

It collects all remaining arguments into an array using the ... syntax.

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

Can rest parameters and optional parameters be used together?

A

Yes, but rest parameters must come last in the parameter list.

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

What is a generic function in TypeScript?

A

A function that works with different types using a placeholder like <T>.</T>

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

What is a constraint on generics in TypeScript?

A

It restricts the allowed types that a generic can accept using extends.

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

Why use generics in functions?

A

To write reusable, type-safe functions that work across multiple types.

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

What’s an example of a generic function?

A

function identity<T>(arg: T): T { return arg; }</T>

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

How do you constrain a generic to accept only objects with a specific property?

A

Use T extends { length: number } to require a length property.

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

What are advantages of optional parameters?

A

Improve flexibility and make function calls simpler when not all data is available.

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

What are disadvantages of optional parameters?

A

May lead to ambiguous function behavior if overused.

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

What are advantages of default parameters?

A

Avoids undefined values and provides fallback behavior without checking manually.

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

What are potential issues with rest parameters?

A

They can lead to unexpected results if argument order is misunderstood.

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

What’s a use case for rest parameters?

A

Variadic functions like sum(...args: number[]) or logging utilities.

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

What’s a use case for generic functions?

A

Functions like map, filter, or utility methods that should work with any type.

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

What’s a use case for generic constraints?

A

Validating properties in objects, like ensuring the presence of an ID or key.

20
Q

What is the impact of generics on system design?

A

They allow for highly abstract and reusable code components, reducing duplication.

21
Q

What is the architectural implication of generic functions?

A

Encourages writing DRY utility functions and shared logic modules.

22
Q

What is a performance implication of using generics?

A

None at runtime—generics are erased during compilation and don’t affect performance.

23
Q

How do generics affect debugging?

A

They provide better type safety at compile time, but don’t exist in runtime logs or stack traces.

24
Q

What’s a tradeoff of overusing generics?

A

Too much abstraction can hurt readability and make the function harder to understand.

25
What’s a common interview question about generics?
How do you create a generic function in TypeScript and what are the benefits?
26
What’s a gotcha with function overloads and generics?
You must ensure all overloads are compatible with the generic implementation.
27
Can you combine default parameters and generics?
Yes, generics and default parameters can be used together to provide type and value flexibility.
28
What’s a good practice for function signatures in shared interfaces?
Declare function types in interfaces for consistency across implementations.
29
What’s a best practice for naming generic types?
Use clear names like T, U, V or meaningful ones like TItem or TResult for better clarity.
30
What is an anti-pattern with optional parameters?
Using too many optional parameters instead of passing a config object can reduce maintainability.
31
What is a gotcha with rest parameters and spread?
Using spread incorrectly can cause unexpected results or pass the wrong number of arguments.
32
What’s the effect of optional parameters on TypeScript’s overload resolution?
They can make overloads harder to resolve if types become ambiguous.
33
How does TypeScript treat unspecified optional parameters?
They are treated as `undefined` if not provided.
34
What is a tip for debugging functions with generics?
Check inferred types with IDE tools or manually annotate generic parameters for clarity.
35
What is a risk of not constraining generics?
Leads to overly permissive logic and runtime errors due to unchecked assumptions.
36
Why are constraints on generics useful in libraries?
They allow developers to build flexible yet safe APIs with guardrails.
37
What’s a performance benefit of TypeScript's generic erasure model?
No runtime overhead for generics—compilation strips them, improving performance.
38
How do default parameters help with backward compatibility?
They allow new parameters without breaking existing function calls.
39
What’s a common interview question about rest parameters?
What are rest parameters and how do they differ from arguments objects?
40
What is a tuple-type rest parameter?
You can specify fixed types for rest parameters like `(...args: [string, number])`.
41
What’s the syntax for function with default and optional params?
function greet(name = 'Guest', age?: number) {}
42
What’s a best practice for combining function overloads and generics?
Use overload signatures for external type safety and a generic implementation signature internally.
43
What is an anti-pattern with generic functions?
Making them too abstract without clear benefits, reducing readability.
44
What’s the benefit of using function signatures in interfaces?
Provides a clear contract and improves testability of function dependencies.
45
How do optional/default parameters impact unit testing?
They allow testing fewer cases when defaults cover common scenarios.
46
What’s a debugging tip for generic constraints?
Temporarily replace the generic with a concrete type to isolate errors.
47
Can you use multiple generic types in a function?
Yes, use syntax like to define multiple type parameters.