如何正确输入配置对象?

huangapple go评论86阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月26日 15:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定