Skip to content

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

Open
@Andarist

Description

@Andarist

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.

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

        Participants

        @RyanCavanaugh@Andarist

        Issue actions

          Return types fail to emit for methods of anonymous plain objects Β· Issue #52415 Β· microsoft/TypeScript