Skip to content

Docs: Optional Parameters and Rest Parameters section is out of date #2448

Closed as not planned
@Pyrolistical

Description

@Pyrolistical

Page URL: https://www.typescriptlang.org/docs/handbook/type-compatibility.html#optional-parameters-and-rest-parameters

Issue:
Now that microsoft/TypeScript#41544 has landed, the sample problem is now solvable.

// Sample problem
function invokeLater(args: any[], callback: (...args: any[]) => void) {
  /* ... Invoke callback with 'args' ... */
}

// Better solution
function invokeLater<T extends any[]>(...argsWithCallback: [...T, (...args: T) => void)]) {
  const args = argsWithCallback.slice(0, -1) as T
  const callback = argsWithCallback[argsWithCallback.length - 1] as (...args: T) => void
  /* ... Invoke callback with 'args' ... */
}

Recommended Fix:

Document feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions