Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useTimeAgo): Support custom UnitNames type for units field #3684

Merged
merged 3 commits into from Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/core/useTimeAgo/index.ts
Expand Up @@ -54,7 +54,7 @@ export interface FormatTimeAgoOptions<UnitNames extends string = UseTimeAgoUnitN
/**
* Custom units
*/
units?: UseTimeAgoUnit<UseTimeAgoUnitNamesDefault>[]
units?: UseTimeAgoUnit<UnitNames>[]
}

export interface UseTimeAgoOptions<Controls extends boolean, UnitNames extends string = UseTimeAgoUnitNamesDefault> extends FormatTimeAgoOptions<UnitNames> {
Expand Down Expand Up @@ -157,7 +157,7 @@ export function formatTimeAgo<UnitNames extends string = UseTimeAgoUnitNamesDefa
max,
messages = DEFAULT_MESSAGES as UseTimeAgoMessages<UnitNames>,
fullDateFormatter = DEFAULT_FORMATTER,
units = DEFAULT_UNITS,
units = DEFAULT_UNITS as UseTimeAgoUnit<UnitNames>[],
showSecond = false,
rounding = 'round',
} = options
Expand All @@ -169,11 +169,11 @@ export function formatTimeAgo<UnitNames extends string = UseTimeAgoUnitNamesDefa
const diff = +now - +from
const absDiff = Math.abs(diff)

function getValue(diff: number, unit: UseTimeAgoUnit) {
function getValue(diff: number, unit: UseTimeAgoUnit<UnitNames>) {
return roundFn(Math.abs(diff) / unit.value)
}

function format(diff: number, unit: UseTimeAgoUnit) {
function format(diff: number, unit: UseTimeAgoUnit<UnitNames>) {
const val = getValue(diff, unit)
const past = diff > 0

Expand Down