0

I am trying to use Monaco editor for editing the body of a function.

I managed provide the arguments and their types using global declarations, but I also would like to declare the type of the this context variable.

I could not find any samples or information online how this could be done. Any idea?

Bharata
  • 13,509
  • 6
  • 36
  • 50
  • 1
    Could you provide a code sample ? There are multiple ways of doing this, the simplest is just a `this` argument to a function: `function n(this:type, .. other params.. ) {}` but context would help – Titian Cernicova-Dragomir Aug 14 '18 at 12:20
  • Thanks for coming back to me, I only try to expose the function body through monaco-editor For example `function(arg1:string){ //Only edit here console.log(this.value1) //To here }` – user1836822 Aug 14 '18 at 12:32
  • https://stackoverflow.com/questions/28920753/declaring-the-type-of-this-in-a-typescript-function/41358367#41358367 – Aleksey L. Aug 14 '18 at 13:05
  • 1
    Possible duplicate of [Declaring the type of 'this' in a typescript function?](https://stackoverflow.com/questions/28920753/declaring-the-type-of-this-in-a-typescript-function) – Aleksey L. Aug 14 '18 at 13:06
  • user1836822, would you like to see my answer and write me a feedback please. If you are satisfied with my answer, please mark it as accepted on the left side from my answer. – Bharata Aug 17 '18 at 17:09

2 Answers2

0

Using your example you should declare this variable first ... like

value1:string = "";

takeValue(arg1:string){

this.value1 = arg1; // here you add the value

}
0

This feature is allready available in TS 2.0.

See official announcement from Microsoft:

https://github.com/Microsoft/TypeScript/pull/6739

For example like follows:

class SomeType
{
    value1 = 'Some string'
}

function x(this:SomeType)
{
    console.log(this.value1)
    //your code...
}
Bharata
  • 13,509
  • 6
  • 36
  • 50