0

I found this piece of code that I don't fully understand. To be precise, I'm talking about those function arguments:

function defaultPort({ port=123, user='test' }: Object = {}) {
}

I thought it was a different kind of deconstructing, but apparently it's not. Also, I can't run this code in Node, so I guess it needs some kind of module to use it properly.

Has anyone seen this before?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Krzysztof Borowy
  • 538
  • 1
  • 5
  • 21
  • 3
    For what it's worth: That's not (vanilla) JavaScript. – Cerbrus Sep 05 '16 at 07:08
  • 3
    It's TypeScript, you need to use `tsc` to turn it into JS. – jonrsharpe Sep 05 '16 at 07:09
  • 3
    *Parts* of that are like modern JavaScript, but the `: Object = {}` at the end, if it's been quoted correctly, is not. – T.J. Crowder Sep 05 '16 at 07:09
  • @jonrsharpe: TypeScript's playground doesn't like it, complains that `Object` doesn't have `port` and `user`. But yeah, could be, with more declarations or something. – T.J. Crowder Sep 05 '16 at 07:11
  • 1
    @T.J.Crowder interesting, it looks like the stuff in http://stackoverflow.com/questions/23314806/setting-default-value-for-typescript-object-passed-as-argument. OP, could you be more specific about where you found it? – jonrsharpe Sep 05 '16 at 07:12
  • It's typescript alright, and there's nothing that is missing in it, just that there's an error there (see my answer below). Not sure why people down voted the question or voted to close it, it's perfectly fine. +1 from me. – Nitzan Tomer Sep 05 '16 at 08:43

2 Answers2

1

As mentioned by TJ, the : Object part is either TypeScript or Flow.

To be precise, I'm talking about those function arguments:

I believe you are talking about default parameters. They allow you to assign default values to the parameters of a function.

Basic usage:

function sayHi (name = 'John') {
  console.log('Hi ' + name)
}

sayHi('James') // Hi James
sayHi() // Hi John

When you are destructuring an object, you can also use default parameters.

Example:

function printInfo ({ name = 'John', age = 30 }) {
  console.log('Name: ' + name)
  console.log('Age: ' + age)
}

printInfo({ name: 'James', age: 10 }) // Name: James, Age: 10
printInfo({ age: 20 }) // Name: John, Age: 20
printInfo({ name: 'Michael' }) // Name: 'Michael', age: 30
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Saad
  • 49,729
  • 21
  • 73
  • 112
0

It's typescript alright, but it's not valid typescript as it produces an error when compiled, and it's deconstructing indeed, or more specifically object destructuring using default values.
The reason that it does not compile is that the type of the parameter is Object which won't allow anything that isn't indexed, for example:

let o: Object = { a: 5 };
console.log(o.a); // Error: Property 'a' does not exist on type 'Object'
console.log(o["a"]); // this is fine

(code in playground)

Change it to any and it will work just fine:

function defaultPort({ port=123, user='test' }: any = {}) { // no error
}

(code in playground)

Though it will probably be best to just declare the type:

type Param = {
    port?: number;
    user?: string;
}

function defaultPort({ port=123, user='test' }: Param = {}) {
}

(code in playground)

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299