What is the correct way to create an object that I don't need a reference to in TypeScript?

323 Views Asked by At

I need to create an object, and I don't need the reference to it once it's created. I have this code:

const myPieChart = new Chart(context, {
    type: "pie",
    data: dataSource
});

But I get the warning 'myPieChart' is declared but its value is never read.ts(6133) and SolarLint says Remove this useless assignment to variable "myPieChart".sonarlint(typescript:S1854)

If I instead use this code:

new Chart(context, {
    type: "pie",
    data: dataSource
});

TSLint complains: unused expression, expected an assignment or function call (no-unused-expression)tslint(1)

Surely there must be a proper way to write this code without just disabling one of the warnings?

1

There are 1 best solutions below

0
On
function main(){
  return new Chart(context, {
    type: "pie",
    data: dataSource
  });
}
main();

or

export const myPieChart = new Chart(context, {
    type: "pie",
    data: dataSource
});