Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this reference in Object #10410

Closed
senyaak opened this issue Aug 18, 2016 · 4 comments
Closed

this reference in Object #10410

senyaak opened this issue Aug 18, 2016 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@senyaak
Copy link

senyaak commented Aug 18, 2016

TypeScript Version: 1.8.0 / nightly (2.0.0-dev.201xxxxx)

Code

interface IFoo {
  get(val: number): number;
  getAll(val: string): number;
}
function foo(): IFoo {

  return {
    get(val: number): number {
      return val + 1;
    },
    getAll(val: string): number {
      return this.get(val)*10;
    }
  };
}

Expected behavior:
Compiler show an error that val string isn't a number
Actual behavior:
No error.

Trick to get expected behavior:

interface IFoo {
  get(val: number): number;
  getAll(val: string): number;
}
function foo(): IFoo {

  var a: IFoo =  {
    get(val: number): number {
      return val + 1;
    },
    getAll(val: string): number {
      return this.get(val)*10;
    }
  };

  return a;
}
@nippur72
Copy link

it seems correct to me as this.get() does not refer to the get() defined in the object, but to a whatever get function that appears in this context when the function will be called.

Try writing this way:

interface IFoo {
  get(val: number): number;
  getAll(val: string): number;
}
function foo(): IFoo {
  const get = function(val: number): number {
      return val + 1;
  };
  const getAll = function(val: string): number {
      return get(val)*10;
  } 
  return {
    get, getAll
  };  
}

And btw, your "Trick to get expected behavior" does not works neither.

@danielearwicker
Copy link

See #6739 for work in this area, though I don't think all of it is going into 2.0.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 18, 2016
@RyanCavanaugh
Copy link
Member

@nippur72 's comment is correct. You can use an arrow function to actually preserve this, or an explicit this type annotation (in TS 2.0) if somehow people are invoking a with your class instance as this (how??)

@senyaak
Copy link
Author

senyaak commented Aug 19, 2016

sorry Trick to get expected behavior should be:

interface IFoo {
  get(val: number): number;
  getAll(val: string): number;
}
function foo(): IFoo {

  var a: IFoo =  {
    get(val: number): number {
      return val + 1;
    },
    getAll(val: string): number {
      return a.get(val)*10;
    }
  };

  return a;
}

:)

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants