打字稿/角度类型声明无用



这种情况现在发生在我身上多次,所以我想问我是否做错了什么,或者Typescript/Angular真的没有这样做。据我了解,最大的优势之一是变量、参数等的类型声明(接口(。

今天我遇到了这个问题:

一个简单的开关按钮:

<p-selectButton [options]="posLayouts" (onChange)="handle($event)" [(ngModel)]="pageSize"></p-selectButton>

变量:

pageSize: number;
  posLayouts: SelectItem[];
  //On mistake I setted the value here as a string
  this.posLayouts.push({label:'1 Woche', value: '1'});
  this.posLayouts.push({label:'3 Wochen', value: '3'});

在 change 函数中,我只需调用另一个值为 posLayouts 的函数。

handle(e){getPager(this.pageSize)}
  getPager(pageSize: number = 3){
    let totalPages: number;
    totalPages = 23 + pageSize -1
    console.log(totalPages)
}

console.log((,返回的不是 eiter 230 或 232,而不是想要的 23 或 25。它采用 pageSize 作为字符串。这种类型的错误很难找到,这次我很幸运,因为它很容易找到......Angular 或 tslint 或我的编辑器都没有告诉我在任何地方我不能将字符串存储在数字变量中。

据我了解,打字稿应该检查兼容性,还是 Angular 以某种方式禁用了这里的收听?还是我做错了什么?

TypeScript 类型仅由开发工具(linter、autofill等(使用。
对于运行时,所有代码都转换为 JavaScript,并且不再有可用的类型信息。
因此,对于来自代码外部的用户输入或其他数据源,您需要确保数据符合预期的类型。

最新更新