I have an array of objects that can be null or a Gamepad:
let pads : (Gamepad | null)[] = navigator.getGamepads()
And if the first entry is a Gamepad (and not null) I want to execute some code
let pad: Gamepad | null = gamepads[0]
if (pad) {
myOtherCode(pad)
}
This null-check works but I have to create an extra temporary variable. Is there a way to just null-check the first entry of the array directly?
if (gamepads[0]) {
myOtherCode(gamepads[0])
}
Argument of type 'Gamepad | null' is not assignable to parameter of type 'Gamepad'. Type 'null' is not assignable to type 'Gamepad'
My other code is just a function that expects a gamepad:
function myOtherCode(g:Gamepad) {
console.log(g)
}