name: Feature Request
about: Suggest an idea
title: 'Named arguments'
labels: 'Named arguments'
assignees: 'somebody'
Now we can use an object to provide named arguments.
function test ({x = 1, y = 2}) {}
But we should create an object to use this.
test({y: 1})
Also, we should add default value equals to an empty object when all fields are optional.
function test ({x = 1, y = 2} = {}) {}
I really don't like it.
I would like to see the simplest way to use named arguments.
function test (x = 1, y = 2) {}
test(y: 1)
This converts to ES like
function test (x = 1, y = 2) {}
test(void 0, 1)
It does not replace the named object arguments, just the sugar for positional arguments.
This is a great feature if we use a couple of optional arguments.
function get (weight?: number, height?: number, top?: number, left?: number) {...}
get(top: 42)
get(height: 13)
get(left: 13)
We could change it to
get(,, 42)
get(, 13)
get(,,, 13)
But, the named argument looks most understandable.
Also, this is a great feature to lock the sense of the argument.
Let's change the order of the get function arguments
// function get (weight?: number, height?: number, top?: number, left?: number) {...}
function get (top?: number, left?: number, weight?: number, height?: number) {...}
get(top: 42) // Ok
get(height: 13) // Ok
get(left: 13) // Ok
get(,, 42) // Bug
get(, 13) // Bug
get(,,, 13) // Bug
Or we can change a couple of argument sense
// function get (weight?: number, height?: number, top?: number, left?: number) {...}
function get (top?: number, right?: number, bottom?: number, left?: number) {...}
get(top: 42) // Ok
get(height: 13) // Error
get(left: 13) // Ok
get(,, 42) // Bug
get(, 13) // Bug
get(,,, 13) // Ok
That's why TypeScript was born.
function setPosition (top?: number, left?: number, ... elements: Element[]) {...}
const element1 = document.createElement('div')
setPosition(top: 0, elements: [element1])
const element2 = document.createElement('span')
setPosition(left: 0, elements: [element1, element2])
My suggestion meets these guidelines:
Duplicate of #467. Used search terms :named arguments
Also, please respect the issue template for feature requests.
function test (x, y): void
function test (y, x): void
function test (a, b): void {}
test(a: 1, b: 2) // Ok
test(x: 1, y: 2) // Error
You can use only names from the function with a body.
We have 2 ways: use variables or limit order.
use variables:
function test (x, y) {}
let z = 0
test(y: z++, x: z)
converts to
function test (x, y) {}
let z = 0
const _y = z++, _x = z
test(_x, _y)
limit order:
function test (x, y, a?, b?) {}
test(x: 1, y: 2) // Ok
test(1, 2, b: 3) // Ok
test(x: 1, y: 2, b: 3) // Ok
test(y: 1, x: 2) // Error, should be the same order
test(1, 2, b: 3, a: 4) // Error, should be the same order
Any issues?
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
Most helpful comment
Duplicate of #467. Used search terms :
named argumentsAlso, please respect the issue template for feature requests.