Skip to content

Latest commit

 

History

History
89 lines (81 loc) · 3.08 KB

user-supplied-optional-dependencies.md

File metadata and controls

89 lines (81 loc) · 3.08 KB

User-Supplied Optional Dependencies

A user-supplied optional dependency is:

  • a function that your function calls,
  • that is passed in as a parameter with a default value,
  • that the caller can override if they need to

For example:

function doSomething(
    input: any,
    // `onError` is a user-supplied optional dependency
    { onError = THROW_THE_ERROR } = OnErrorOptions = {}
) {
    if (!somethingWorked(input)) {
        throw onError(new SomethingWentWrong());
    }
}

// if we call it like this, `doSomething()` uses the default value
// for the dependency, which is `THROW_THE_ERROR`
doSomething(input);

// if we call it like this, `doSomething()` will use our arrow function
// instead
doSomething(input, { onError: (x) => { logger.logError(x); throw x; });

User-supplied optional dependencies go into the user-supplied options object object.