Skip to content

Methods that return a specific range of numbers should have more accurate return types #2512

@ahkhanjani

Description

@ahkhanjani

E.g. .day() returns a number ranged from 0 to 6, yet the return type is number. It should instead be 0 | 1 | 2 | 3 | 4 | 5 | 6. If this doesn't cause any real issues that I'm unaware of, it can be beneficial in some situations. For example:

const WEEKDAYS = ['Sunday', 'Monday', ..., 'Friday'] as const;

WEEKDAYS[dayjs().day()]; // Currently can be undefined.

With the more accurate return type there will be no need for a runtime check or an exclamation mark.

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