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's really being asked
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.
The full answer
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.
The mistakes people make
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.
What usually comes next
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?
A 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.
Interview question
You need a type alias for a function that takes a string and an optional number parameter defaulting to 5. Which is valid?
- a.type F = (s: string, n: number = 5) => boolean
- b.type F = (s: string, n?: number) => booleanCorrect
- c.type F = function(s: string, n?: number): boolean
- d.type F = (string, number?) => boolean
Why? this is the answer
Function type expressions can mark parameters as optional with ? but cannot encode default values, which are implementation-only details erased at compile time. Option A is invalid because default values belong in the function body, not the type annotation.
Just read this? Test yourself on what you have been reading.
Read the original → typescriptlang.org
- #typescript
- #function-types
- #type-aliases
- #optional-parameters
- #type-system
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.
See open roles