Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I handle the ts error "Type 'number | undefined' is not assignable to type 'number'." ? #168

Closed
takeItIzzy opened this issue Jul 30, 2020 · 1 comment

Comments

@takeItIzzy
Copy link

const regions =
  data &&
  d3
    .hierarchy('data')
    .sum((d) => 1)
    .sort((a, b) => b.value - a.value);

The callback of sort() will throw an error Object is possibly 'undefined'. when I use typeScript, and if I add a condition like this:

const regions =
  data &&
  d3
    .hierarchy('data')
    .sum((d) => 1)
    .sort((a, b) => {
      if (a.value && b.value) {
        return b.value - a.value;
      }
    });

It'll say Type 'number | undefined' is not assignable to type 'number'. How can I handle with it?

@Fil
Copy link
Member

Fil commented Jul 30, 2020

Please address D3 typescript queries to https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/d3

PS: I am surprised by the quotes around 'data' in the hierarchy() call.

@Fil Fil closed this as completed Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants