noUnusedLocals 在函数中不起作用*
noUnusedLocals not working in function*
编译器选项:
"noUnusedLocals": 是的,
"noUnusedParameters":是的,
不在功能中工作。例如我得到错误:
export class AllReduxSagas {
[ts] 属性 'someService' 已声明,但其值从未被读取。
constructor(private someService: SomeService) {}
watchSaga = function* watchSaga() {
yield takeEvery(ACTION_TYPE.SOME_ACTION, this.someSaga, this.someService);
};
...
}
someService 无法被编译器识别,但是当我删除上述编译器选项时 - 一切正常。
为什么会出现这种情况,以及如何解决这个问题。
问题是 watchSaga
不是 class 的成员函数,它是一个具有函数值的字段。因此 watchSaga
函数内部的 this
不一定引用包含 class (this
将在函数内部键入为 any
)
考虑将函数设为成员函数:
export class AllReduxSagas {
constructor(private someService: SomeService) { }
*watchSaga() {
yield this.someService;
};
}
或者如果出于某种原因你想坚持使用类型函数语法的字段,你可以显式键入 this
(尽管这并不一定意味着传递的 this
将是class,它仍然是 function
而不是 =>
箭头函数)
watchSaga = function* watchSaga(this: AllReduxSagas) {
yield this.someService;
};
编译器选项: "noUnusedLocals": 是的, "noUnusedParameters":是的, 不在功能中工作。例如我得到错误:
export class AllReduxSagas {
[ts] 属性 'someService' 已声明,但其值从未被读取。
constructor(private someService: SomeService) {}
watchSaga = function* watchSaga() {
yield takeEvery(ACTION_TYPE.SOME_ACTION, this.someSaga, this.someService);
};
...
}
someService 无法被编译器识别,但是当我删除上述编译器选项时 - 一切正常。 为什么会出现这种情况,以及如何解决这个问题。
问题是 watchSaga
不是 class 的成员函数,它是一个具有函数值的字段。因此 watchSaga
函数内部的 this
不一定引用包含 class (this
将在函数内部键入为 any
)
考虑将函数设为成员函数:
export class AllReduxSagas {
constructor(private someService: SomeService) { }
*watchSaga() {
yield this.someService;
};
}
或者如果出于某种原因你想坚持使用类型函数语法的字段,你可以显式键入 this
(尽管这并不一定意味着传递的 this
将是class,它仍然是 function
而不是 =>
箭头函数)
watchSaga = function* watchSaga(this: AllReduxSagas) {
yield this.someService;
};