iamkun / iamkun/dayjs

1.10.5 has weird type bug on duration.add()

Open
#1,500 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

```ts
import dayjs from 'dayjs'
import duration, { Duration } from 'dayjs/plugin/duration'
dayjs.extend(duration)

type MyType = { duration?: Duration }
const obj: MyType = {}
dayjs.duration(10, 'second').add(obj.duration ?? 0) // fail

const duration: Duration | undefined = undefined
dayjs.duration(10, 'second').add(duration ?? 0) // pass
```

play ground link: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAEwIYE8BWBnOAzKERwDky6GhAUKJLIgK5RIzAQB2ANHAN5wAi9jzFnAC+OPAWKpMAejAAbWgHNgLaQn5NWFEpgB0AUwAeMfSwQAKdQ00sAlOXIwUYfXACyKACrPXAXi501oIA-ABcvBqCIuQAxqwY8BAARmjhHt4ucP6cwuQ6GLpWAqzmAIwADByEGPpxZoS2ukgIFslohZGscMHBcOW2cNLSOEjAcg51CYHFLOF8QV0APnC0ZvrYKvoIWStrGyxbeVIFRTZllUQ1dQgNTS2WnUI9fQNDcGBIGBjkQA

dayjs.duration(10, 'second').add(obj.duration ?? 0) will throw compile error:
```
No overload matches this call.
The last overload gave the following error.
Argument of type 'Duration | 0' is not assignable to parameter of type 'Duration'.
Type 'number' is not assignable to type 'Duration'.(2769)
```

I suspect this some weird typescript type infer error

however, can you just allow duration.add(undefined) to pass compiler, which equiv as duration.add(0)

Contributor guide

Open the contributing guide

Research direction

Start with the TypeScript declarations for the dayjs/plugin/duration entry point and reproduce the reported calls from the issue. Check how add() handles Duration, numbers, and undefined, then verify that the requested call type-checks without changing the runtime behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.