Typescript: Named arguments

Created on 1 Aug 2020  路  3Comments  路  Source: microsoft/TypeScript


name: Feature Request
about: Suggest an idea
title: 'Named arguments'
labels: 'Named arguments'
assignees: 'somebody'


Search Terms

  • Named arguments

Suggestion

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.

Use Cases

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.

Examples

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])

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Duplicate

Most helpful comment

Duplicate of #467. Used search terms :named arguments

Also, please respect the issue template for feature requests.

All 3 comments

Duplicate of #467. Used search terms :named arguments

Also, please respect the issue template for feature requests.

issue 1

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.

issue 2

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanzer picture seanzer  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

uber5001 picture uber5001  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments