Language reference · section 19
19. Labelled arguments
Feature: Labelled arguments
Scenario: A call can name each argument
Given a function connect with parameters host, port, and secure
When the caller writes connect(host: "db", port: 5432, secure: true)
Then each value goes to the named parameter
Scenario: Named arguments can come in any order
Given the call connect(secure: true, host: "db")
Then the compiler accepts the call
Scenario: Positional arguments come before labelled ones
Given the call connect(port: 5432, "db")
Then the compiler reports an error "positional arguments must come before labelled ones"
Scenario: A label cannot name a parameter twice
Given the call connect(host: "a", host: "b")
Then the compiler reports an error "`connect` is given more than once"
Scenario: A label must name a parameter
Given the call connect(port: 1, timeout: 3) where connect has no timeout parameter
Then the compiler reports an error "`connect` has no parameter named `timeout`"
Scenario: A parameter with a default can be left out
Given a parameter secure with a default value
When the caller leaves secure out
Then the default value applies
Scenario: A parameter without a default needs a value
Given a call that leaves out a parameter with no default
Then the compiler reports an error "`connect` needs a value for `host`"
Scenario: Labels work only when the callee is a name
Given a call with labels on a member expression
Then the compiler reports an error "labelled arguments only work when calling a function by its name"
Scenario: A spread cannot be mixed with labels
Given a call with a spread and a label
Then the compiler reports an error "a spread cannot be mixed with labelled arguments"
The .d.ts file keeps the positional signature. TypeScript callers pass arguments in order.