With TypeScript 5.9.3, type checking fails in projects that use workbox-precaching when exactOptionalPropertyTypes is enabled:
./node_modules/workbox-precaching/PrecacheFallbackPlugin.d.ts:18:15 - error TS2420: Class 'PrecacheFallbackPlugin' incorrectly implements interface 'WorkboxPlugin'.
Types of property 'handlerDidError' are incompatible.
Type 'HandlerDidErrorCallback | undefined' is not assignable to type 'HandlerDidErrorCallback'.
Type 'undefined' is not assignable to type 'HandlerDidErrorCallback'.
The problem is in workbox-precaching/PrecacheFallbackPlugin.d.ts:
declare class PrecacheFallbackPlugin implements WorkboxPlugin {
// ...
handlerDidError: WorkboxPlugin['handlerDidError'];
}
In workbox-core/types.d.ts, handlerDidError is optional:
export declare interface WorkboxPlugin {
// ...
handlerDidError?: HandlerDidErrorCallback;
// ...
}
Because handlerDidError is optional, WorkboxPlugin['handlerDidError'] resolves to HandlerDidErrorCallback | undefined.
With exactOptionalPropertyTypes enabled, TypeScript distinguishes between the case where a property is not present and the case where it is present and explicitly set to undefined (JavaScript also has subtly different behaviour in these two cases, so this TypeScript setting sometimes catches actual bugs).
The interface declaration of WorkboxPlugin specifies that handlerDidError is optional, but that if it is present then it must have the type HandlerDidErrorCallback.
HandlerDidErrorCallback | undefined is not assignable to HandlerDidErrorCallback, therefore PrecacheFallbackPlugin does not implement the WorkboxPlugin interface correctly.
PrecacheFallbackPlugin should instead be declared like:
declare class PrecacheFallbackPlugin implements WorkboxPlugin {
// ...
handlerDidError: HandlerDidErrorCallback;
}
or possibly
declare class PrecacheFallbackPlugin implements WorkboxPlugin {
// ...
handlerDidError?: HandlerDidErrorCallback;
}
Or, alternatively, if workbox allows explicitly setting optional properties to undefined, then undefined should be added to the types of optional properties, for example:
export declare interface WorkboxPlugin {
// ...
handlerDidError?: HandlerDidErrorCallback | undefined;
// ...
}
With TypeScript 5.9.3, type checking fails in projects that use workbox-precaching when
exactOptionalPropertyTypesis enabled:./node_modules/workbox-precaching/PrecacheFallbackPlugin.d.ts:18:15 - error TS2420: Class 'PrecacheFallbackPlugin' incorrectly implements interface 'WorkboxPlugin'. Types of property 'handlerDidError' are incompatible. Type 'HandlerDidErrorCallback | undefined' is not assignable to type 'HandlerDidErrorCallback'. Type 'undefined' is not assignable to type 'HandlerDidErrorCallback'.The problem is in
workbox-precaching/PrecacheFallbackPlugin.d.ts:In
workbox-core/types.d.ts,handlerDidErroris optional:Because
handlerDidErroris optional,WorkboxPlugin['handlerDidError']resolves toHandlerDidErrorCallback | undefined.With
exactOptionalPropertyTypesenabled, TypeScript distinguishes between the case where a property is not present and the case where it is present and explicitly set toundefined(JavaScript also has subtly different behaviour in these two cases, so this TypeScript setting sometimes catches actual bugs).The interface declaration of
WorkboxPluginspecifies thathandlerDidErroris optional, but that if it is present then it must have the typeHandlerDidErrorCallback.HandlerDidErrorCallback | undefinedis not assignable toHandlerDidErrorCallback, thereforePrecacheFallbackPlugindoes not implement theWorkboxPlugininterface correctly.PrecacheFallbackPluginshould instead be declared like:or possibly
Or, alternatively, if workbox allows explicitly setting optional properties to
undefined, thenundefinedshould be added to the types of optional properties, for example: