mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
Simplify implementation of firstOrUndefined and lastOrUndefined (#22572)
This commit is contained in:
1 file changed
+2
-2
@@ -1077,7 +1077,7 @@ namespace ts {
|
||||
* Returns the first element of an array if non-empty, `undefined` otherwise.
|
||||
*/
|
||||
export function firstOrUndefined<T>(array: ReadonlyArray<T>): T | undefined {
|
||||
return elementAt(array, 0);
|
||||
return array.length === 0 ? undefined : array[0];
|
||||
}
|
||||
|
||||
export function first<T>(array: ReadonlyArray<T>): T {
|
||||
@@ -1089,7 +1089,7 @@ namespace ts {
|
||||
* Returns the last element of an array if non-empty, `undefined` otherwise.
|
||||
*/
|
||||
export function lastOrUndefined<T>(array: ReadonlyArray<T>): T | undefined {
|
||||
return elementAt(array, -1);
|
||||
return array.length === 0 ? undefined : array[array.length - 1];
|
||||
}
|
||||
|
||||
export function last<T>(array: ReadonlyArray<T>): T {
|
||||
|
||||
Reference in new issue
Block a user