iamkun / iamkun/dayjs

[bug] [typescript] subtract function has conflicts if several plugins were used

Open
#2,517 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

## Describe the bug

```
import weekOfYear from 'dayjs/plugin/weekOfYear';
import quarterOfYear from 'dayjs/plugin/quarterOfYear';

dayjs.extend(weekOfYear);
dayjs.extend(quarterOfYear);

// good
dayjs().subtract(1, 'week');
// good
dayjs().subtract(1, 'quarter');

type TMySpecificTimeUnitTypeForDropdownOrWhatever = 'quarter' | 'week';

function whatever(timeUnit: TMySpecificTimeUnitTypeForDropdownOrWhatever) {
// error
dayjs().subtract(1, timeUnit);
}
```

TS:
```
No overload matches this call.
Overload 1 of 3, '(value: number, unit: QUnitType): Dayjs', gave the following error.
Overload 2 of 3, '(value: number, unit?: ManipulateType | undefined): Dayjs', gave the following error.ts(2769)
```

plugins creates new types
```
export type OpUnitType = UnitType | "week" | "weeks" | 'w';
export type QUnitType = UnitType | "quarter" | "quarters" | 'Q';
```

and add override to subtract function, which means you cannot create unified type with types from different plugins

## Expected behavior
plugins should not create new override, but rather extends existing type

Contributor guide

Open the contributing guide

Research direction

Search the TypeScript declarations for the subtract overloads and the OpUnitType and QUnitType definitions used by the weekOfYear and quarterOfYear plugins. Reproduce the reported union-type example, then verify that enabling both plugins allows a shared 'quarter' | 'week' unit without an overload error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.