Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript TS2683和#x9;(TS)及"x27 ;;这';隐式具有类型';任何';因为它没有类型注释_Typescript - Fatal编程技术网

Typescript TS2683和#x9;(TS)及"x27 ;;这';隐式具有类型';任何';因为它没有类型注释

Typescript TS2683和#x9;(TS)及"x27 ;;这';隐式具有类型';任何';因为它没有类型注释,typescript,Typescript,我在使用TypeScript文件时遇到了这个问题,我想知道如何解决这个问题 现在我已经抑制了这个typescript异常,但是我想学习如何解决这个问题。以下是我的代码: export class BaseResult { isSuccessful: boolean; totalRecords: number; successMessage: string; reasonForFailure: string; lastExecutedDateTime: Da

我在使用TypeScript文件时遇到了这个问题,我想知道如何解决这个问题

现在我已经抑制了这个typescript异常,但是我想学习如何解决这个问题。以下是我的代码:

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
,而不是type
any


请参阅以获取更多解释。

使用显式
注释:
页面=函数(此:BaseResult){
页面=函数(){
替换为
页面(){
更改箭头函数:页面=()=>类语法不需要
函数或
()=>
声明方法的符号。请注意,如果您确实需要指定的调用方
this
(例如,您的函数将由一个框架调用,该框架将
this
设置为合理的值),您可以通过引入一个伪
this
参数:
callback=function来告诉TypeScript this
的类型(this:HTMLElement){…}
我正在使用它(当我将函数传递给第三方库时)。它修复了我最初的问题,但现在eslint不高兴
错误解析错误:意外的关键字“this”
没有引用的规则,我可以重写以允许它。有人有什么想法吗?你的函数是某种类方法吗?对于自由函数
注释导致错误