JavaScript Optional deconstruction function parameter

How do you modify the function below to make the second parameter optional?

TypeScript:

function getName(name: string, {
    lastName
  }: {
    lastName: string
  }) {
  // ...
}

getName('John'); // error

Update:

The solution I've found so far is to take out the deconstruction into the function body:

function getName(name: string, options: {
    lastName: string
  } = {} as any) {
    const { lastName } = options;
    // ...
}

getName('John'); // OK

However, I still cannot find how to make it work in this context:

const getName = Bluebird.coroutine(function* co(name: string,
  {
    lastName
  }: {
    lastName: string
  }) {
    // ...
});

getName('John'); // error


/* -------- DECLARATIONS -------- */

declare namespace Bluebird {
    interface CoroutineOptions {
        yieldHandler(value: any): any;
    }
}

declare class Bluebird<R> {
    static coroutine<T, A1, A2>(
        generatorFunction: (a1: A1, a2: A2) => IterableIterator<any>,
        options?: Bluebird.CoroutineOptions
    ): (a1: A1, a2: A2) => Bluebird<T>;
}

Moving the deconstruction to the function body still gives an error:

const getName = Bluebird.coroutine(function* co(name: string, options: {
    lastName: string
  } = {} as any) {
    // ...
});

getName('John'); // error: Expected 2 arguments but got 1.
Answer:1

You will need to define the interface for the lastName property as optional when defining the options object. If no options is defined, the default object is an empty object {}.

function foo(required: string, options: { lastName?: string } = {}) {
    console.log(required);
    if (options.lastName) {
        console.log(options.lastName);
    }
}

foo('foo1')
foo('foo2', {})
foo('foo3', {lastName: 'bar'})

Running the above, the console output is:

foo1
foo2
foo3
bar

See TypeScript playground link to try it out yourself.

Answer:2

I have a a component that needs to check for multiple options and return based on those options. Therefore I have created an outside function that is called in my component return statement to ...

I have a a component that needs to check for multiple options and return based on those options. Therefore I have created an outside function that is called in my component return statement to ...

  1. return from function python
  2. return from function bash
  3. return from function javascript
  4. return from function matlab
  5. return from function r
  6. return from function powershell
  7. return from function vba
  8. return from function ruby
  9. return from function in shell script
  10. return from function rust
  11. return from function gdb
  12. return from function lisp
  13. return from function c
  14. return from function golang
  15. return from function kotlin
  16. return from function js
  17. return from function php
  18. return from function elixir
  19. return from function java
  20. return from function react

I have a Web API and I'm trying to get JSON Data from it by using Vue, but I get neither data or errors, so I don't what is wrong. I want to load the data when the page is loaded. Here is my code: ...

I have a Web API and I'm trying to get JSON Data from it by using Vue, but I get neither data or errors, so I don't what is wrong. I want to load the data when the page is loaded. Here is my code: ...

  1. fetch data with react hooks
  2. fetch data with react
  3. fetch data with axios
  4. fetch data with javascript
  5. fetch data with redux
  6. fetch data with ajax
  7. fetch data with redux thunk
  8. fetch data with mysqli php
  9. fetch data with ajax in php
  10. fetch data with usereducer
  11. fetch data with pdo
  12. fetch data with jquery
  13. fetch data with redux saga
  14. fetch data with react redux
  15. fetch data with php
  16. fetch data with json
  17. fetch data with angular
  18. fetch data with post
  19. fetch data with jquery ajax
  20. fetch data with sql

I am facing a little problems with using Custom Palettes with Angular Material Framework. I still not understand myself to use custom theme. In angular config file. $mdThemingProvider.definePalette('...

I am facing a little problems with using Custom Palettes with Angular Material Framework. I still not understand myself to use custom theme. In angular config file. $mdThemingProvider.definePalette('...

How can I detect width and height of the camera to use it on canvas and maintain the proportions?? I need to detect width and height of the camera and then use it on a canvas. In the new canvas I ...

How can I detect width and height of the camera to use it on canvas and maintain the proportions?? I need to detect width and height of the camera and then use it on a canvas. In the new canvas I ...

  1. detect height width