Python-style kwargs in TypeScript 2025-09-21 PythonTypeScript
My TypeScript function signatures always start small. There's usually an optional argument or two, but the call sites are easy to reason about:
const greet = ( name : string , prefix = "Hello" ) => ` ${ prefix } , ${ name } . ` ; greet ( "Alice" ) ; greet ( "Bob" , "!" ) ;
But as these functions grow, it's cumbersome to specify the last optional argument while using defaults for the rest:
const greet = ( name : string , prefix = "Hello" , ending = "." , extraNames : string [ ] | undefined = undefined ) => ` ${ prefix } , ${ name } ${ extraNames ?. length ? ` , ${ extraNames . join ( ", " ) } ` : "" } ${ ending } ` ; greet ( "Alice" , undefined , "!" ) ; greet ( "Alice" , undefined , undefined , [ "Bob" , "Charlie" ] ) ;
Those floating undefined s are also impossible to reason about unless your development environment can show you the underlying argument names. Not a great reading or debugging experience.
Python's solution is to let callers specify the names of arguments (which is why they're called keyword args, since they're specified by their keyword):
def greet ( name : str , prefix = "Hello" , ending = "." , extra_names : list [ str ] | None = None , ) : . . . greet ( "Alice" , prefix = "Howdy" ) greet ( "Alice" , extra_names = [ "Bob" , "Charlie" ] )
This makes it easy to write clear, concise function calls.
And, while there's not a 1:1 analogue in TypeScript, we can actually get pretty close. All it takes are 3 objects:
... continue reading