microsoft / microsoft/TypeScript

Return types fail to emit for methods of anonymous plain objects

Open
#52,415 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and the example in the issue, then inspect how declaration emit handles the anonymous object returned by test(). Determine a defined outcome for the this type, an expanded anonymous type, or a diagnostic, and verify that the generated .d.ts is correct without introducing portability or circularity problems.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.