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

Type for Color missing string[] for indexable options #9461

Closed
jcoryalvernaz opened this issue Jul 22, 2021 · 9 comments
Closed

Type for Color missing string[] for indexable options #9461

jcoryalvernaz opened this issue Jul 22, 2021 · 9 comments
Labels
type: bug type: types Typescript type changes
Milestone

Comments

@jcoryalvernaz
Copy link

Expected Behavior

The Color type should support indexable options.

Current Behavior

The Typescript compiler will return the following error when providing an array of strings for an indexable color property (e.g. pointBackgroundColor):

Type 'string[]' is not assignable to type 'Color'

Possible Solution

Update the Color type definition to include an option for string[], as was previously done with DefinitleyTyped

@etimberg etimberg added the type: types Typescript type changes label Jul 22, 2021
@etimberg
Copy link
Member

I'm not sure the definitely typed versions are correct. According to their types, the following is valid but it is won't be parsed correctly by https://github.com/kurkle/color.

const dataset = {
  pointBackgroundColor: [
    ['red', 'green'],
    ['blue', 'yellow']
  ]
}

Are you able to provide a full example showing how this format is used? I tested in using the latest dev code and the colours were not correctly applied

@jcoryalvernaz
Copy link
Author

@etimberg I'm not sure that is correct. The DefinitelyTyped version allowed for an array of strings like so:

const dataset = {
  pointBackgroundColor: ['red', 'green'],
}

Which according to the Chart.js docs is an indexable option. This worked fine in v2 using the DefinitelyTyped type definitions. Now fails in v3 using the built-in type definitions.

@etimberg
Copy link
Member

That is supported in v3 as well using. The definitely typed versions say that ChartColor[] is also allowed hence the nested arrays of strings being accepted in those definitions.

I just tested locally in our TS tests (using ts 4.3.5) and there were no errors. What TS version are you using?

const chart = new Chart('id', {
  type: 'line',
  data: {
    labels: [],
    datasets: [{
      data: [],
      pointBackgroundColor: ['red', 'green'],
    }]
  },
});

@jcoryalvernaz
Copy link
Author

@etimberg We're also using 4.3.5. I'm not sure how TS isn't complaining since the type definition for Color doesn't look like it accepts an array type.

@etimberg etimberg added this to the Version 3.5.0 milestone Jul 22, 2021
@etimberg
Copy link
Member

Color itself doesn't, but the PointPrefixedOptions that contains pointBackgroundColor is transformed such that each property takes an array or a function as well. There have been a number of issues with TS >= 4.2.0 that were fixed in #9444. These were caused by changes inside of TS itself.

That PR hasn't been released yet, so v3.4.1 would only work as far as TS v4.1.x. This should be resolved once 3.5.0 releases

@kurkle
Copy link
Member

kurkle commented Jul 23, 2021

If your chart type is 'radar', I sent #9426 to fix the types

@jcoryalvernaz
Copy link
Author

@etimberg Ahh. I see. I'll keep my eye out for 3.5 then. Thanks for the quick reply and awesome work on this project!

@jcoryalvernaz
Copy link
Author

@kurkle I was using the line chart type

@etimberg
Copy link
Member

3.5.0 is released so this should be working now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug type: types Typescript type changes
Projects
None yet
Development

No branches or pull requests

3 participants