question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistencies in type checking functions with optional destructured parameters

See original GitHub issue

I encountered unexpected and inconsistent errors when attempting to write a function declaration for a function with an optional destructured parameter with default property values when using strictNullChecks.

It’s possible that I wrote my function declaration incorrect but I couldn’t find a good example for writing function declarations with an optional destructured parameter with default property values. Regardless, the inconsistency in behavior described in this issue is still a bug AFAICT.

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code Bug demo (enable strictNullChecks to see error): https://www.typescriptlang.org/play/#src=const want %3D ({a %3D 1%2C b %3D 2} %3D {}) %3D> {}%3B want()%3B want({})%3B want({a%3A 3})%3B want({a%3A 3%2C b%3A 4})%3B want({b%3A 5})%3B let have%3A (nums%3F%3A {a%3F%3Anumber%2C b%3F%3Anumber}) %3D> void%3B have %3D want%3B have %3D ({a %3D 1%2C b %3D 2} %3D {}) %3D> {}%3B %2F%2F Same value as want. have()%3B have({})%3B have({a%3A 3})%3B have({a%3A 3%2C b%3A 4})%3B have({b%3A 5})%3B

let have: (nums?: {a?:number, b?:number}) => void;
      have = ({a = 1, b = 2} = {}) => {};  // A. Results in error.
const want = ({a = 1, b = 2} = {}) => {};
have = want;                               // B. This works fine.

Expected behavior: The lines denoted by A. and B. both work (preferable). OR The lines denoted by A. and B. fail in the same way.

Actual behavior: The line denoted by A. results in errors:

Type '{ a?: number | undefined; b?: number | undefined; } | undefined' has no property 'a' and no string index signature.
Type '{ a?: number | undefined; b?: number | undefined; } | undefined' has no property 'b' and no string index signature.

The line denoted by B. works.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sandersncommented, Oct 10, 2017

In the meantime, both bugs can be worked around by adding an explicit type annotation to the destructured parameter itself: const want = ({ a = 1, b = 2 }: { a?: number, b?: number} = {}) => {};

0reactions
Lazarus535commented, Jan 26, 2018

PR is out! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typing Destructured Object Parameters in TypeScript
Here's the TypeScript syntax for typing the destructured properties. ... The toJSON function accepts a value of any type that should be ...
Read more >
JSDoc support for destructured parameters · Issue #11859
As a JS developer relying on destructuring for function parameters, this issue generates a lot of false "problems" when mixing parameters with and...
Read more >
Can you have optional destructured arguments in a ...
I'd like to write a function that takes an object argument, uses destructuring in the function signature, and have that argument be optional...
Read more >
Destructured function parameters and code maintainability
Well, fine! I guess we can add a new argument to the function. We can even try to make the function signature backwards...
Read more >
Prevent Error with Default {} when Destructuring
When you use destructuring, make sure to set a default value of empty {} to prevent it from throwing an error! function hi(person)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found