sindresorhus / p-queue

Promise queue with concurrency control
MIT License
3.39k stars 182 forks source link

TypeScript TS2344 error with enabled "exactOptionalPropertyTypes" setting. #201

Closed aikawayataro closed 8 months ago

aikawayataro commented 8 months ago

When using p-queue from my project where exactOptionalPropertyTypes is enabled in tsconfig, I've encountered a TS2344 error:

node_modules/p-queue/dist/index.d.ts:10:88 - error TS2344: Type 'PriorityQueue' does not satisfy the constraint 'Queue<RunFunction, EnqueueOptionsType>'.
  Types of property 'filter' are incompatible.
    Type '(options: Readonly<Partial<PriorityQueueOptions>>) => RunFunction[]' is not assignable to type '(options: Partial<EnqueueOptionsType>) => RunFunction[]'.
      Types of parameters 'options' and 'options' are incompatible.
        Type 'Partial<EnqueueOptionsType>' is not assignable to type 'Readonly<Partial<PriorityQueueOptions>>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
          Types of property 'priority' are incompatible.
            Type 'EnqueueOptionsType["priority"]' is not assignable to type 'number'.
              Type 'number | undefined' is not assignable to type 'number'.
                Type 'undefined' is not assignable to type 'number'.

10 export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = QueueAddOptions> extends EventEmitter<EventName> {
                                                                                          ~~~~~~~~~~~~~

The problem here is conflicting options' types:

Queue.filter has options: Partial<Options> PriorityQueue.filter has options: Readonly<Partial<PriorityQueueOptions>>

Changing Queue.filter's options type to options: Readonly<Partial<Options>> resolves the problem for me.