Skip to content

Commit 1736741

Browse files
author
Andy
authored
Simplify implementation of firstOrUndefined and lastOrUndefined (#22572)
1 parent 5f1668e commit 1736741

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ namespace ts {
10771077
* Returns the first element of an array if non-empty, `undefined` otherwise.
10781078
*/
10791079
export function firstOrUndefined<T>(array: ReadonlyArray<T>): T | undefined {
1080-
return elementAt(array, 0);
1080+
return array.length === 0 ? undefined : array[0];
10811081
}
10821082

10831083
export function first<T>(array: ReadonlyArray<T>): T {
@@ -1089,7 +1089,7 @@ namespace ts {
10891089
* Returns the last element of an array if non-empty, `undefined` otherwise.
10901090
*/
10911091
export function lastOrUndefined<T>(array: ReadonlyArray<T>): T | undefined {
1092-
return elementAt(array, -1);
1092+
return array.length === 0 ? undefined : array[array.length - 1];
10931093
}
10941094

10951095
export function last<T>(array: ReadonlyArray<T>): T {

0 commit comments

Comments
 (0)