Can anyone explain this?
Why is undefined not an option if type is a union of empty array and array of data when accessing an item
type emptyArray = []
type accessEmptyArray = emptyArray[0] // returns undefined
type validData = {id:number, name:string}
type someData = validData[] | []
type test = someData[0] // returns type validData but could also be undefined
I assume I can solve this by using an assertion but it will make my code quite verbose.
I'm quite surprised by this issue as it seems like a problem that is easily overlooked and the type of problem that TS is built to solve but maybe I haven't thought of a downside to my assumed way of it working
Example: Playground