我在用格朗特任务写TypeScript。我正在尝试翻译我在JavaScript中已经拥有的东西。
所以,当grunt运行一个任务时,它会运行一个函数。运行时,grunt会将this设置为具有有用属性的对象,就像jQuery用您正在处理的元素重载this一样。我可以访问有用的属性,如this.files;
grunt.registerMultiTask('clean', function() {
    this.files.forEach(function(f) { Delete(f); });
});因此,“删除this.files中的所有文件”。
但是,在TypeScript中,我不知道您是否可以向编译器“提示”this是一种特定类型,因此我无法获得intellisense。我如何告诉TypeScript将this视为一种不同的类型?
发布于 2015-03-07 22:41:32
我如何告诉TypeScript把这看作是另一种类型
您可以通过声明一个this参数来实现这一点。对于您的用例,我添加了this: {files:any[]}
grunt.registerMultiTask('clean', function(this: {files:any[]}) {
    this.files.forEach(function(f) { Delete(f); });
});更多
发布于 2016-12-28 08:45:54
发布于 2018-02-02 16:29:34
虽然我发现现在可以这样做:
class ClassyClass {
    prop = 'Juicy Strings'
}
function x( this: ClassyClass ) {
    console.log( this.prop )
}我更喜欢在争论中不占用房地产的另一种选择
function x() {
    const that: ClassyClass = this
    console.log( that.prop )
}https://stackoverflow.com/questions/28920753
复制相似问题