Accepted answer

What you see here is the beauty of TypeScript as it warns you during compile time that one part of your chain could return undefined and therefore lead to an error that aborts your script.

Use a guard clause to show the compiler you checked for undefined values.

if (!Highcharts?.getOptions()?.colors) {

// ... here comes your code
// the compiler will no longer show errors

Or make sure to have a proper default value, like this:

let color = '#615E9B';
if (Highcharts?.getOptions()?.colors) {
  color = Highcharts.getOptions().colors[0];

// Your code, but use color variable instead of Highcharts.getOptions().colors[0]

That is also possible inline, but hard to read and to maintain. I would suggest you to stick to few more lines of code that are easy to understand.


I tried to reproduce your issue and it seems fine:

Related Query

More Query from same tag