Skip to content

--noImplicitThis code fix? #28964

@JoshuaKGoldberg

Description

@JoshuaKGoldberg

Search Terms

noImplicitThis noImplicitAny code fix this annotation

Suggestion

#14786 added a code fix for --noImplicitAny failures. Can we have the same for --noImplicitThis?

In cases where the this can be inferred for all locations that directly call a function, can TypeScript make a best guess of what the this type is meant to be?

Use Cases

In older code that uses fancy function assignments and isn't yet onboarded onto --noImplicitThis, it can be a bit of a pain to add them in.

Examples

interface NameBox {
  name: string;
  printName(): void;
}

// Code fix suggestion to add a `this: NameBox`?
function printName() {
  console.log(this.name);
}

const createNameBox = (name: string): NameBox => ({
    name,
    printName,
});

This example seems like it could be an inferrable one, since we can tell that printName is used as a member of a NameBox.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions