typescript 在打字稿函数中声明“this”的类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28920753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:58:14  来源:igfitidea点击:

Declaring the type of 'this' in a typescript function?

typescript

提问by Steve Cooper

I'm writing a grunt taskin TypeScript. I'm trying to translate something I already have in JavaScript.

我正在用 TypeScript编写一个grunt 任务。我正在尝试翻译我在 JavaScript 中已有的东西。

So, when grunt runs a task, it runs a function. When it runs, grunt sets thisto an object with useful properties, the same way that jQuery overloads thiswith the element you are working on. I could access useful properties like this.files;

所以,当 grunt 运行一个任务时,它运行一个函数。当它运行时,grunt 设置this为一个具有有用属性的对象,就像 jQuerythis用你正在处理的元素重载一样。我可以访问有用的属性,例如this.files

grunt.registerMultiTask('clean', function() {
    this.files.forEach(function(f) { Delete(f); });
});

So, "delete all the files in this.files".

因此,“删除”中的所有文件this.files

However, in TypeScript, I don't know if you can 'hint' to the compiler that thisis a particular type, so I don't get intellisense. How do I tell TypeScript to consider thisto be a different type?

但是,在 TypeScript 中,我不知道您是否可以“暗示”this特定类型的编译器,因此我没有得到智能感知。我如何告诉 TypeScript 将其视为this不同的类型?

采纳答案by basarat

How do I tell TypeScript to consider this to be a different type

我如何告诉 TypeScript 将其视为不同的类型

There isn't a dedicated syntax for this yet but there are talks about it : https://github.com/Microsoft/TypeScript/issues/229

目前还没有专门的语法,但有关于它的讨论:https: //github.com/Microsoft/TypeScript/issues/229

For now what you have done is effectively the only way although I would not use an assertionand just use a type annotation:

现在你所做的是有效的唯一方法,尽管我不会使用断言而只使用类型注释:

var self:grunt.task.IMultiTask<string> = this;
self.files.forEach(function (f) {

});

回答by Aleksey L.

Now (from TS 2.0) you can specify function's thistype by using fake thisparameter (should be the first one):

现在(从 TS 2.0 开始)您可以this通过使用假这个参数(应该是第一个)来指定函数的类型:

grunt.registerMultiTask('clean', function(this: SomeType) {
    //...
});

thisparameters are fake parameters that come first in the parameter list of a function

this参数是函数参数列表中最先出现的假参数

More info here

更多信息在这里

回答by Jason

While I found that is now possible with this:

虽然我发现现在可以这样做:

class ClassyClass {
    prop = 'Juicy Strings'
}

function x( this: ClassyClass ) {
    console.log( this.prop )
}

I have come prefer an alternative that doesn't take up real estate in the arguments line

我更喜欢在参数行中不占用房地产的替代方案

function x() {
    const that: ClassyClass = this

    console.log( that.prop )
}

回答by Steve Cooper

I have a bit of an answer. I can do this;

我有点答案。我可以做这个;

var self = <grunt.task.IMultiTask<string>>this;
self.files.forEach(function (f) {

});

which works OK. It's gonna have consequences, like not being able to write arrow functions...

这工作正常。它会产生后果,比如无法编写箭头函数......