类型 'unknown' 不可分配给类型 'KeyboardEvent'.ts (2345)
Type 'unknown' is not assignable to type 'KeyboardEvent'.ts (2345)
我正在学习 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)
只需告诉 ts 这个流将是键盘事件流
const logger = fromEvent<KeyboardEvent>(this.input.nativeElement, 'keyup');
这个<Type>
参数使记录器Observable<KeyboardEvent>
并且ts不会抱怨它
我正在学习 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)
只需告诉 ts 这个流将是键盘事件流
const logger = fromEvent<KeyboardEvent>(this.input.nativeElement, 'keyup');
这个<Type>
参数使记录器Observable<KeyboardEvent>
并且ts不会抱怨它