英文:
How to correctly type the configuration Object?
问题
I have a working graph (i.e. It shows what it is supposed to show and behaves as expected on interactions).
Since I work in TypeScript, I get TS errors on callbacks in the definition of the configuration object, for instance:
(...)
point: {
events: {
click: function () {
console.log(this)
emit('result', phishingMap.value[this.category]) // the error is here
}
}
}
(...)
This piece of code yields the error
Property 'category' does not exist on type '{ click: () => void; }'. TS(2339)
How can I fix that by hinting about the content of this
? The Highcharts documentation for TS does not mention any special steps to get the types declarations in.
EDIT: I know about traditional function and fat arrows (and the behaviour of this
) but the problem is not in how to access the data, but about the data typing.
英文:
I have a working graph (i.e. It shows what it is supposed to show and behaves as expected on interactions).
Since I work in TypeScript, I get TS errors on callbacks in the definition of the configuration object, for instance:
(...)
point: {
events: {
click: function () {
console.log(this)
emit('result', phishingMap.value[this.category]) // the error is here
}
}
}
(...)
This piece of code yields the error
Property 'category' does not exist on type '{ click: () => void; }'. TS(2339)
How can I fix that by hinting about the content of this
? The Highcharts documentation for TS does not mention any special steps to get the types declarations in.
EDIT: I know about traditional function and fat arrows (and the behaviour of this
) but the problem is not in how to access the data, but about the data typing.
答案1
得分: 1
To fix the TypeScript error and provide type information for the this context inside the callback function, you can use an arrow function instead of a regular function.
点这里:{
事件:{
单击:() => {
console.log(this);
emit('result', phishingMap.value[this.category]);
}
}
}
如果您仍然遇到类型推断的问题,您可以显式注释此的类型以提供 TypeScript 额外的信息。例如:
点这里:{
事件:{
单击:function(this: Highcharts.Chart, event: Highcharts.ChartClickEvent) {
console.log(this);
emit('result', phishingMap.value[this.category]);
}
}
}
英文:
To fix the TypeScript error and provide type information for the this context inside the callback function, you can use an arrow function instead of a regular function.
point: {
events: {
click: () => {
console.log(this);
emit('result', phishingMap.value[this.category]);
}
}
}
If you're still encountering issues with type inference, you can explicitly annotate the type of this to provide TypeScript with additional information. For example:
point: {
events: {
click: function(this: Highcharts.Chart, event: Highcharts.ChartClickEvent) {
console.log(this);
emit('result', phishingMap.value[this.category]);
}
}
}
答案2
得分: 0
这里,“this”指的是你正在编写的对象。它与Highcharts无关,而与JS在编写对象内部的函数时的工作方式有关。
为了保持引用,你应该使用箭头函数
(...)
point: {
events: {
click: () => {
console.log(this)
emit('result', phishingMap.value[this.category]) // the error is here
}
}
}
(...)
这是经典函数和箭头函数之间的主要区别:
第一个使用第一个父对象的引用
第二个使用类的引用
英文:
Here, this
refer to the object you are writting. It has nothing to do with Highcharts but with how JS works while writting a function inside an object.
To keep the reference, you should use an arrow function
(...)
point: {
events: {
click: () => {
console.log(this)
emit('result', phishingMap.value[this.category]) // the error is here
}
}
}
(...)
Here is the big difference between a classic function and an arrow function :
The first one use the ref of the first parent
The second one use the ref of the class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论