在TypeScript中创建不需要引用的对象的正确方法是什么?

问题描述

我需要创建一个对象,并且一旦创建就不需要引用。我有以下代码

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

但是我收到警告'myPieChart' is declared but its value is never read.ts(6133),SolarLint说Remove this useless assignment to variable "myPieChart".sonarlint(typescript:S1854)

如果我改用此代码

new Chart(context,data: dataSource
});

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

肯定有一种正确的方法来编写此代码,而不仅仅是禁用警告之一?

解决方法

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

export const myPieChart = new Chart(context,data: dataSource
});