繁体   English   中英

“数字”类型的参数不可分配给“从不”类型的参数,为什么我会收到此错误?

[英]Argument of type 'number' is not assignable to parameter of type 'never' why i am getting this error?

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
 showSecret = false;

logArray = [];

onToggleDetails()
{
this.showSecret = !this.showSecret;
this.logArray.push(this.logArray.length);
}

}

运行上述代码时,Angular 给出以下错误错误:src/app/app.component.ts:16:20 - 错误 TS2345:“数字”类型的参数不可分配给“从不”类型的参数。

this.logArray.push(this.logArray.length);

编译器希望您声明 logArray 的类型。 尝试以下操作:

logArray: number[] = [];

让我知道它是否有效:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM