Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Indeed, how can you declare a list with some elements nullable, and some not?

The result should instead be a tuple, but IDK how well tuple size inference would work in a case like that.



I’m not at a computer now, but you can explicitly define a tuple type like:

    type Tuple<T, K> = [T, K | null];
Which is my first thought, but I can’t test it against the compiler at the moment and I’m not sure if I’m missing something.

...JavaScript would allow you to extend that list during runtime (unless you freeze it)

    type Tuple<T, K> = [T, K]
    const Tuple<T, K> = (x: T, y: K): Tuple<T, K> => {
        const tup = [x, y];
        Object.freeze(tup);
        return tup;
    };


Having it be a tuple would require variadic generics, wouldn't it?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: