Optional deconstruction function parameter

227
December 07, 2017, at 08:47 AM

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

options = {} should work?

function getName(name: string, options = {}) {}

Rent Charter Buses Company
READ ALSO
Typescript code not showing anything in the browser after transpiling

Typescript code not showing anything in the browser after transpiling

I am trying to write some very simple examples of Typescript code In Atom editorThe example consists of one main

245
Javascript Image preview disappeared after canceling file browser window

Javascript Image preview disappeared after canceling file browser window

I'm trying to upload a file with FileReader() object, Everything is perfect but when I cancel the file browser after last one is still previewing, that will be disappearedHere is my codes:

263
&ldquo;buttonImportIcon&rdquo; syntax for Javascript PDF code

“buttonImportIcon” syntax for Javascript PDF code

I am attempting to import an icon into a button form field, using a page from a separate PDF fileI need to use other pages rather than just the first page of the document and I have tried to accomplish this by specifying "nPage" in the syntax

251
Simplest way to set the scale of an SVG using panResponder in React Native with d3

Simplest way to set the scale of an SVG using panResponder in React Native with d3

This feels like it should be simple, but I'm banging my head against the wall to get it going

358