Skip to content

[vitest-pool-workers] inject() in pool options no longer infers types or checks keys #15137

Description

@petebacondarwin

Split out of #15123 so that PR can land. Raised by Devin: #15123 (comment)

Problem

WorkerPoolOptionsContext.inject was changed from typeof inject (vitest's) to a widened signature in packages/vitest-pool-workers/src/pool/plugin.ts:

inject: <T = unknown>(key: string) => T;

The change was deliberate, and the reason is documented in place: binding to typeof inject from this package's copy of vitest makes keyof ProvidedContext resolve to never, because a consumer's declare module "vitest" augmentation only applies to the consumer's own copy, and pnpm can resolve two separate virtual-store instances. So the previous typing was broken in its own way.

The cost is that inference and key checking are both gone:

  • The return type now defaults to unknown, so anywhere the value is passed to a typed parameter or assigned to a typed variable, consumers need an explicit type argument. Our own fixture had to become inject<number>("echoServerPort") in fixtures/vitest-pool-workers-examples/hyperdrive/vitest.config.ts.
  • key is string, so a misspelled key is no longer a type error.

There is also a semver question: this is a change to a public type that can require consumers to edit their config, which is not obviously a patch.

Possible direction

Keep an overload that preserves inference for known keys while still accepting arbitrary strings, e.g.:

inject: {
  <K extends keyof ProvidedContext>(key: K): ProvidedContext[K];
  <T = unknown>(key: string): T;
};

This needs checking against the actual failure it was working around — if keyof ProvidedContext resolves to never in the cross-copy case, the first overload may simply never match and the fallback would still apply, but that wants verifying with a real pnpm-resolved consumer rather than assumed.

Acceptance

  • Consumers using a ProvidedContext augmentation get inference and key checking back, without reintroducing the never resolution problem.
  • If the public type ends up changing in a way consumers must react to, the changeset is classified accordingly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    package:vitestRelating to the Workers Vitest integration

    Type

    No type

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions