Type alias for a function with optional param and default value
Tests whether you know function type expressions cannot encode default values. A great answer writes (s: string, n?: number) => boolean, explains defaults are implementation-only, and notes parameter names are required.
WHAT THIS TESTS: This question probes your understanding of TypeScript function type expressions and the boundary between type signatures and implementation details. At the senior level, interviewers care less about exact syntax recall and more about whether you know what the type system can and cannot represent. They want to see that you understand type aliases describe call shapes, not runtime behavior, and that constructs like default parameter values belong to the function implementation, not its type annotation.
A GOOD ANSWER COVERS: First, the correct function type expression syntax, for example type Validator = (input: string, maxLength?: number) => boolean. Second, an explicit statement that default values cannot be expressed in a type alias because types are erased at compile time; the default belongs in the function body, not the type. Third, a reminder that parameter names are mandatory in function type expressions, so writing (string, number?) => boolean would be incorrect. Fourth, a brief mention that the question mark makes the parameter optional, distinguishing this from a required parameter whose type includes undefined.
COMMON WRONG ANSWERS: Trying to embed a default value directly into the type alias such as (s: string, n: number = 5) => boolean, which is a syntax error. Omitting parameter names and writing only the types, which changes the meaning or fails to compile. Using a function declaration style instead of a function type expression, for example type F = function(s: string, n?: number): boolean, which is not the standard idiom for a simple callable type. Confusing call signatures in object types with function type expressions, which is unnecessary for this scenario.
LIKELY FOLLOW-UPS: Can you express a default value at the type level? The answer is no, because types do not exist at runtime. What is the difference between an optional parameter and a required parameter typed as number or undefined? Would this type alias work for a method on a class, and if not, how would you write it as an interface call signature? How do rest parameters fit into function type expressions?
ONE CONCRETE EXAMPLE: Suppose you are building a form validation library. You might define type StringCheck = (value: string, minChars?: number) => boolean. The implementing function could then supply its own default: const isLongEnough: StringCheck = (value, minChars = 3) => value.length >= minChars. The type alias only knows that minChars is optional and must be a number if provided; the value 3 is invisible to the type system.
Read the original → typescriptlang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.