Typescript TS2683和#x9;(TS)及"x27 ;;这';隐式具有类型';任何';因为它没有类型注释
我在使用TypeScript文件时遇到了这个问题,我想知道如何解决这个问题 现在我已经抑制了这个typescript异常,但是我想学习如何解决这个问题。以下是我的代码:Typescript TS2683和#x9;(TS)及"x27 ;;这';隐式具有类型';任何';因为它没有类型注释,typescript,Typescript,我在使用TypeScript文件时遇到了这个问题,我想知道如何解决这个问题 现在我已经抑制了这个typescript异常,但是我想学习如何解决这个问题。以下是我的代码: export class BaseResult { isSuccessful: boolean; totalRecords: number; successMessage: string; reasonForFailure: string; lastExecutedDateTime: Da
export class BaseResult {
isSuccessful: boolean;
totalRecords: number;
successMessage: string;
reasonForFailure: string;
lastExecutedDateTime: Date;
}
export class Result<T> extends BaseResult {
data: T;
}
export class CollectionResult<T> extends BaseResult {
data: T[];
}
export class PagedCollectionResult<T> extends CollectionResult<T> {
pageNumber: number;
pageSize: number;
filter: string;
pages = function () {
return (this.totalRecords <= 0 || this.pageSize <= 0) ? 0 : Math.ceil(this.totalRecords / this.pageSize);//<--All the **this** keyword shows the error
}
}
导出类BaseResult{
isSuccessful:布尔值;
总数记录:数字;
成功消息:字符串;
失败原因:字符串;
lastExecutedDateTime:日期;
}
导出类结果扩展了BaseResult{
资料:T;
}
导出类CollectionResult扩展了BaseResult{
数据:T[];
}
导出类PagedCollectionResult扩展CollectionResult{
页码:页码;
页面大小:数字;
过滤器:字符串;
页面=函数(){
return(this.totalRecords正如某些注释所示,您的this
引用没有被键入,因为您正在使用函数(){}
定义函数的语法。此类函数中的this
对象本质上属于any
类型,因为this
将是函数的调用者(在设计时不可知)
如果将语法更改为箭头函数,如
pages = () => {
或者干脆省略function关键字和箭头,如
pages() {
然后函数中的this
对象将引用类实例this
,而不是typeany
请参阅以获取更多解释。使用显式此注释:页面=函数(此:BaseResult){
将页面=函数(){
替换为页面(){
更改箭头函数:页面=()=>类语法不需要函数或()=>
声明方法的符号。请注意,如果您确实需要指定的调用方this
(例如,您的函数将由一个框架调用,该框架将this
设置为合理的值),您可以通过引入一个伪this
参数:callback=function来告诉TypeScript this
的类型(this:HTMLElement){…}
我正在使用它(当我将函数传递给第三方库时)。它修复了我最初的问题,但现在eslint不高兴错误解析错误:意外的关键字“this”
没有引用的规则,我可以重写以允许它。有人有什么想法吗?你的函数是某种类方法吗?对于自由函数此注释导致错误