Skip to content

SwRegistrationOptions documented as an interface but declared as an abstract class #72263

Backlog

Description

@airbreather

Describe the problem that you experienced

The documentation for SwRegistrationOptions is internally inconsistent about what it is, and the way it's presented encourages a pattern that violates the expectations the class keyword creates for readers — a disconnect surfaced by the new typescript-eslint rule @typescript-eslint/no-unsafe-class-instance-assignment.

Specifically:

  1. The getting-started guide calls SwRegistrationOptions an interface:

    Angular service workers support comprehensive configuration options through the SwRegistrationOptions interface […]

  2. The API reference page for the same name declares it as an abstract class:

    abstract class SwRegistrationOptions {
      enabled?: boolean | undefined;
      updateViaCache?: ServiceWorkerUpdateViaCache | undefined;
      type?: WorkerType | undefined;
      scope?: string | undefined;
      registrationStrategy?: string | (() => Observable<unknown>) | undefined;
    }
  3. Both pages then demonstrate passing plain object literals where this abstract class type is expected — e.g. provideServiceWorker('ngsw-worker.js', { enabled: !isDevMode(), scope: '/app/' }) — which only compiles because TypeScript assigns classes structurally, ignoring the class-ness the declaration implies to readers.

A reader who sees a class (abstract or not) reasonably expects class semantics: derived behavior via getters, constructor logic/invariants, subclassing, instanceof. Nothing here is intended to work that way — it is documented and used purely as a structural shape, with the class/abstract form serving only to make the type usable as a legacy DI provider token ({ provide: SwRegistrationOptions, useFactory: ... }).

Enter the URL of the topic with the problem

Describe what you were looking for in the documentation

I was looking for the correct way to pass registration options to provideServiceWorker() and for a precise definition of what SwRegistrationOptions is (interface vs. class), so I could judge whether the documented pattern is safe to use under strict type-checking and linting (typescript-eslint with type-checked rules, including the newly added no-unsafe-class-instance-assignment, which flags object literals assigned where a class instance is expected).

Describe the actions that led you to experience the problem

  1. Followed the getting-started guide and added the documented provideServiceWorker('ngsw-worker.js', { enabled: ..., scope: ... }) call to my app config.
  2. Enabled the new typescript-eslint rule @typescript-eslint/no-unsafe-class-instance-assignment.
  3. The documented pattern was flagged: an object literal is passed where an instance of the (abstract) class SwRegistrationOptions is expected.
  4. Checked the two documentation pages linked above and found one calls the type an "interface" while the other declares it an abstract class.

Describe what you want to experience that would fix the problem

Any of the following would resolve it, in descending order of preference:

  1. API change (root cause): Declare SwRegistrationOptions as an interface and, if the legacy provide: SwRegistrationOptions DI usage must keep working, keep a class/token only for that purpose — or migrate that example to a modern InjectionToken<SwRegistrationOptions>. That would make the documented provideServiceWorker(..., { ... }) pattern fully consistent with the declared type.
  2. Docs-only fix (minimal): Make the two pages agree on what the type is, and explicitly document that SwRegistrationOptions is intentionally declared as an abstract class solely so it can serve as a DI token, that instances are never constructed, and that plain object literals are the supported (and expected) way to supply options — so readers and linters don't misinterpret it as a class with instance semantics.

Add a screenshot if that helps illustrate the problem

N/A — the discrepancy is textual between the two linked pages.

If this problem caused an exception or error, please paste it here

/PATH_TO_MY_PROJECT/src/app/app.config.ts
  37:7  error  This object literal is used where an instance of class `SwRegistrationOptions` is expected  @typescript-eslint/no-unsafe-class-instance-assignment

(Rule docs: https://typescript-eslint.io/rules/no-unsafe-class-instance-assignment — "reports values that aren't created by a class's constructor but are used where an instance of the class is expected".)

If the problem is browser-specific, please specify the device, OS, browser, and version

Not browser-specific.

Provide any additional information here in as much as detail as you can

  • TypeScript's structural assignability means any object literal satisfying the public member shape passes even when the target is a class; the new eslint rule treats class-provenance as a nominal signal instead. Angular's own API only works under the structural interpretation, while the class declaration signals instance semantics to readers — the two interpretations conflict, and the docs sit on both sides of the fence (calling it an "interface" on one page while declaring an abstract class on the other).
  • The abstract modifier suggests the class exists mainly to prevent new SwRegistrationOptions() while remaining usable as a DI token (the API page shows exactly that legacy usage in its Description section). That's a reasonable legacy constraint, but it should be stated, or the design should move to interface + InjectionToken.
  • Reference to the eslint rule's own "When Not To Use It" guidance, which describes this exact situation as an anti-pattern to avoid declaring: "If you use classes as plain shapes, such as data containers whose constructors don't do anything important, you might prefer to declare those shapes as interfaces or type aliases instead."
  • Verified against angular.dev at v22.3.0 (site footer) as of 1 Dec 2026.
  • Everything above this bullet point was drafted with the assistance of an AI assistant. I read every single word to confirm that I wouldn't be annoyed if I received this report.

Activity

  1. added
    area: service-worker
    on Dec 1, 2026
  2. added this to the needsTriage milestone on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: service-worker

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions