Skip to content

Named arguments #39860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
d8corp opened this issue Aug 1, 2020 · 3 comments
Closed
5 tasks done

Named arguments #39860

d8corp opened this issue Aug 1, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@d8corp
Copy link

d8corp commented Aug 1, 2020


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:

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

Duplicate of #467. Used search terms :named arguments

Also, please respect the issue template for feature requests.

@d8corp
Copy link
Author

d8corp commented Aug 1, 2020

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?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 3, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants