类型“未知”不可分配给类型“KeyboardEvent”.ts (2345)

时间:2021-02-15 23:31:20

标签: angular typescript events keyboard

我正在学习 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)

1 个答案:

答案 0 :(得分:1)

告诉 ts 这个流将是键盘事件流

clear
input float total_jobs_industry str39 industry str8 month
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Nov_2020"
38 "Computer Hardware & Software"         "Dec_2020"
12 "Consulting"                           "Dec_2020"
63 ""                                     "Dec_2020"
32 "IT Services"                          "Dec_2020"
32 "IT Services"                          "Nov_2020"
38 "Computer Hardware & Software"         "Nov_2020"
12 "Aerospace & Defense"                  "Nov_2020"
12 "Accounting"                           "Nov_2020"
12 "Accounting"                           "Dec_2020"
end 

duplicates drop 

gen mdate = monthly(month, "MY")

levelsof mdate, local(months)
tokenize "`c(Mons)'" 
foreach m of local months { 
    local month = month(dofm(`m'))
    label def mdate `m' "``month''", modify 
}
label val mdate mdate 

set scheme s1color 
graph hbar (asis) total_jobs_industry, over(mdate) over(industry, sort(1) descending) 

这个 const logger = fromEvent<KeyboardEvent>(this.input.nativeElement, 'keyup'); 参数使记录器 <Type> 并且 ts 不会抱怨它