Skip to content

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

@aikawayataro

Description

@aikawayataro

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions