Skip to content

Implement interface codefix is offered when the implemented interface type is invalid #63465

@danyalahmed1995

Description

@danyalahmed1995

🔎 Search Terms

implement interface codefix invalid type argument unresolved type symbol fixClassIncorrectlyImplementsInterface

🕗 Version & Regression Information

This is the behavior in the current repository state.

I found existing fourslash tests that appear to document this as undesirable behavior, but the tests currently still expect the codefix to be available:

  • tests/cases/fourslash/codeFixClassImplementInterfaceTypeParamInstantiateError.ts
  • tests/cases/fourslash/codeFixClassImplementInterfaceUndeclaredSymbol.ts

⏯ Playground Link

N/A: this is a language service codefix issue, reproduced through existing fourslash/codefix tests.

💻 Code

interface I<T extends string> {
  x: T;
}

class C implements I<number> { }

A second repro:

interface I {
  x: T;
}

class C implements I { }

🙁 Actual behavior

class C implements I {
x: T;
}

The resulting code still has semantic errors because the implemented interface reference or member type was already invalid.

🙂 Expected behavior

The `Implement interface` codefix should not be offered when the implemented interface reference or required member types contain semantic errors, such as an invalid type argument or an unresolved type symbol.

### Additional information about the issue

Nearby fourslash tests already contain comments indicating these cases should use `verify.codeFixAvailable([])`:

- `tests/cases/fourslash/codeFixClassImplementInterfaceTypeParamInstantiateError.ts`
- `tests/cases/fourslash/codeFixClassImplementInterfaceUndeclaredSymbol.ts`

The likely code path is:

- `src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts`

The fix appears to be created from `checker.getTypeAtLocation(implementedTypeNode)` without rejecting implemented type nodes that have related semantic errors.

Focused test commands used locally:

```sh
npx.cmd hereby runtests --tests=tests/cases/fourslash/codeFixClassImplementInterfaceTypeParamInstantiateError.ts --light=false
npx.cmd hereby runtests --tests=tests/cases/fourslash/codeFixClassImplementInterfaceUndeclaredSymbol.ts --light=false

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions