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

Add undefined to return type of array-last #447

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/array-last/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
* // => undefined
*/
export default function last<T>(arr: readonly [...any, T]): T;
export default function last<T>(arr: T[]): T;
export default function last<T>(arr: T[]): T | undefined;
14 changes: 7 additions & 7 deletions packages/array-last/index.tests.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import last from './index'

// OK
const test1: number = last([1, 2, 3, 4, 5]);
const test2: number[] = last([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const test3: { d: number } = last([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]);
const test4: RegExp = last(["a", 1, true, /r/g]);
const test5: number = last([1]);
const test1: number | undefined = last([1, 2, 3, 4, 5]);
const test2: number[] | undefined = last([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const test3: { d: number } | undefined = last([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]);
const test4: RegExp | undefined = last(["a", 1, true, /r/g]);
const test5: number | undefined = last([1]);
const test6: undefined = last([]);

// make sure it works with readonly arrays
Expand All @@ -14,10 +14,10 @@ const test7: 5 = last([1, 2, 3, 4, 5] as const);
// make sure it works with dynamic arrays and not just static ones
const dynArr = [1, 2, 3];
dynArr.push(4);
const test8: number = last(dynArr);
const test8: number | undefined = last(dynArr);
const dynArr2: (number | string)[] = [1, 2];
dynArr2.push("hi");
const test9: number | string = last(dynArr2);
const test9: number | string | undefined = last(dynArr2);

// Not OK
// @ts-expect-error
Expand Down