Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

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

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

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

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

More info here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...