Skip to content

Return types fail to emit for methods of anonymous plain objectsΒ #52415

Open

Description

Bug Report

πŸ”Ž Search Terms

declaration emit this any

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export function test() {
  return {
    relyingOnThis() {
      return this;
    },
  };
}

// seen and recognized locally in TS files
test().relyingOnThis();

πŸ™ Actual behavior

// generated .d.ts
export declare function test(): {
    relyingOnThis(): any;
};

πŸ™‚ Expected behavior

Make this type to work in such contexts and emit that:

declare function test(): {
    relyingOnThis(): this; // currently reports `A 'this' type is available only in a non-static member of a class or interface.(2526)`
};

Or emit the content of the anonymous type there somehow (when possible, be cautious with circularities, generics etc):

declare const _type: {
    relyingOnThis(): typeof _type;
}

declare function test(): typeof _type

Or just emit a diagnostic that this isn't portable and that an explicit annotation is needed.

Activity

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

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions