diff --git a/src/compat/array/last.spec.ts b/src/compat/array/last.spec.ts index e3b8bcdc9..e7c0ff77d 100644 --- a/src/compat/array/last.spec.ts +++ b/src/compat/array/last.spec.ts @@ -46,4 +46,11 @@ describe('last', () => { expect(last('123')).toBe('3'); expect(last(args)).toBe(3); }); + + it('should ignore negative index properties when the array is empty', () => { + const array: number[] = []; + array['-1'] = 1; + + expect(last(array)).toBe(undefined); + }); }); diff --git a/src/compat/array/last.ts b/src/compat/array/last.ts index 36422c3e6..9548bd160 100644 --- a/src/compat/array/last.ts +++ b/src/compat/array/last.ts @@ -25,7 +25,7 @@ import { isArrayLike } from '../predicate/isArrayLike.ts'; * // noElement will be undefined */ export function last(array: ArrayLike | null | undefined): T | undefined { - if (!isArrayLike(array)) { + if (!isArrayLike(array) || array.length === 0) { return undefined; } return lastToolkit(toArray(array));