Skip to content

Optional destructured parameters do not work with default values #17080

Closed

Description

@jqyu

TypeScript Version: 2.4.0

Code

The following code incorrectly type checks:

interface Attrs {
    foo?: number;
    bar?: number;
}

function unsafe({ foo, bar }: Attrs = {}): number {
    return foo * bar.toString().length + 1;
}

Expected behavior:
This should fail to type check since foo and bar may both be of type undefined

Actual behavior:

Match behaviour of omitting default value:

function failsToTypecheck({ foo, bar }: Attrs): number {
    return foo * bar.toString().length + 1;
}

Or behaviour of omitting destructuring:

function failsToTypecheck(attrs: Attrs = {}): number {
    return attrs.foo * attrs.bar.toString().length + 1;
}

Activity

  1. added
    Needs Investigation
    on Aug 28, 2017
  2. mhegazy commented on Aug 28, 2017

    @mhegazy
    Contributor

    Nathan Shively-Sanders (@sandersn) we talked about this one before, any thoughts here?

  3. whatisaphone commented on Sep 29, 2017

    @whatisaphone

    Here's another example of this that I wrote up before searching for an existing issue:

    interface Foo {
      readonly bar?: number;
    }
    
    function performFoo({ bar }: Foo = {}) {
      useBar(bar);  // `bar` should have type `number | undefined`, so this shouldn't compile.
    }
    
    function useBar(bar: number) {
      alert('The number is: ' + bar);
    }
    
    performFoo();

    We've started using this pattern in our code, so I'm also interested in a fix for this.

  4. whatisaphone commented on Oct 3, 2017

    @whatisaphone

    If someone takes up fixing this, here is another potentially useful test case:

    interface Foo {
      readonly bar?: number;
    }
    
    function performFoo({ bar = null }: Foo = {}) {
      useBar(bar);  // `bar` should have type `number | null`, so this call shouldn't compile.
    }
    
    function useBar(bar: number | undefined) {
      alert('The number is: ' + bar);
    }
    
    performFoo();
  5. added
    Bug
    and removed
    Needs Investigation
    on Oct 10, 2017
  6. added a commit that references this issue on Jan 22, 2018
    18e1ac0
  7. Lazarus535 commented on Jan 22, 2018

    @Lazarus535
    Contributor

    I fixed the issue in PR #21328
    Update: Added testcases, signed CLA
    Update2: Issue #17667 is not (totally) fixed by this. Only the part with the optionality (inclusion of undefined).

  8. added 3 commits that reference this issue on Jan 22, 2018
    5a87a94
    d113418
    7b449a5
  9. added a commit that references this issue on Jan 22, 2018
    a3387cc
  10. added
    Fixed
    on Jan 24, 2018
  11. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugFixed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions