Skip to content

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

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

Open
Andarist opened this issue Jan 25, 2023 · 0 comments
Open

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

Andarist opened this issue Jan 25, 2023 · 0 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@Andarist
Copy link
Contributor

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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants