我确信我能找到答案,但我没有用好的措辞来搜索,对不起。
我在nodejs上使用typescript。我无法更改typescript编译设置,因为我在团队中工作。
我使用了一个具有回调定义的库,比如:(arg1:string, arg2:number)=> void
我有一个函数,它用定义做一些事情,比如:(arg:number)=>void
如何在不出现错误的情况下绑定函数:error TS6133: 'variable' is declared but its value is never read.
这是我之前写的一段代码:
class MyClass{
private libInstance : ExternalLibClass;
constructor() {
this.libInstance = new ExternalLibClass();
this.libInstance.on('event1', this.functionA);
this.libInstance.on('event2', this.functionB);
}
private this.functionA (num : number): void {
// do something
console.log(num);
}
private this.functionB (ignore :string, num : number) : void {
this.functionA(num);
}
}当我编译时,我得到了关于函数B的错误:
`error TS6133: 'ignore' is declared but its value is never read.`谢谢
发布于 2018-02-15 00:06:03
好的,我在the github TypeScript issue 9459中找到了答案
如果变量名的前缀是undescore,则规则TS6133不适用。
class MyClass{
private libInstance : ExternalLibClass;
constructor() {
this.libInstance = new ExternalLibClass();
this.libInstance.on('event1', this.functionA);
this.libInstance.on('event2', this.functionB);
}
private this.functionA (num : number): void {
// do something
console.log(num);
}
private this.functionB (_ignore :string, num : number) : void {
this.functionA(num);
}
}https://stackoverflow.com/questions/48785536
复制相似问题