I've got an array filled with objects that may be either null or a Gamepad:
let devices: (Gamepad | null)[] = navigator.getGamepads()
If the first item in the array happens to be a valid Gamepad, I need to perform a specific action:
let device: Gamepad | null = devices[0]
if (device) {
handleDevice(device)
}
The current solution involves creating an additional temporary variable for null-checking. Is there any way to directly check the first entry of the array for null?
if (devices[0]) {
handleDevice(devices[0])
}
Argument of type 'Gamepad | null' is not assignable to parameter of type 'Gamepad'. Type 'null' is not assignable to type 'Gamepad'
The function I'm calling accepts a gamepad as its argument:
function handleDevice(g:Gamepad) {
console.log(g)
}