镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Cannot reference an object within one of its methods' default arguments #11628

Closed

Description

TypeScript Version: 2.0.3

Code

let Obj = {
  get () {},
  set ( value = Obj.get () ) {}
};

Obj.missing (); // Obj is treated as `any`, so I don't get any error for the usage of the undefined method `missing`

Expected behavior:

I expect to get an error for the usage of the undefined method Obj.missing.

Actual behavior:

I don't get any, since if I use Obj within a default argument inside one of Obj's own methods the entire object type becomes any.

Ugly workaround:

let _self;
let Obj = _self = {
  get () {},
  set ( value = _self.get () ) {}
};

Obj.missing (); // I get an error as expected

Activity

  1. mhegazy commented on Oct 17, 2016

    @mhegazy
    Contributor

    I expect to get an error for the usage of the undefined method Obj.missing.

    you would under --noImplicitAny.

    c:\ts>tsc --noImplicitAny a.ts
    a.ts(1,5): error TS7022: 'Obj' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
    a.ts(3,9): error TS7022: 'value' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
  2. fabiospampinato commented on Oct 18, 2016

    @fabiospampinato
    Author

    Why should I be forced to use --noImplicitAny or to write ugly workarounds when it seems clear to me that the problem is that TypeScript should be a little smarter about this particular case?

    Maybe I'm wrong about it, but why should this behavior be considered the correct one?

  3. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions