【发布时间】:2021-05-18 20:35:59
【问题描述】:
我正在学习 TypeScript 和 Angular,并且在我的书中遇到了一个无法编译的示例。恐怕我不知道如何解决这个问题。
这是无法编译的函数:
ngOnInit(): void {
const logger = fromEvent(this.input.nativeElement, 'keyup');
logger.pipe(
map((evt: KeyboardEvent) => evt.key.charCodeAt(0)),
filter(code => {
if (this.numeric) {
return !(code > 31 && (code < 48 || code > 57));
}
return true;
}),
tap(digit => this.keys += String.fromCharCode(digit))
).subscribe();
}
不会编译的具体行是:
map((evt: KeyboardEvent) => evt.key.charCodeAt(0)),
我看到以下错误:
Argument of type 'OperatorFunction<KeyboardEvent, number>' is not assignable to parameter of type 'OperatorFunction<unknown, number>'.
Type 'unknown' is not assignable to type 'KeyboardEvent'.ts(2345)
【问题讨论】:
标签: angular typescript events keyboard