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

Yes. You can give pickType a type in TS. Something like pickType<B extends boolean>(b: B): B extends true ? string : number.

I’d love to read a post explaining how TS conditional types are or are not a form of dependent types. Or, I’d like to understand dependent types well enough to write that post.



I asked GPT-5 about it before writing the above comment and got a pretty interesting answer about how, for example, you can’t write a function that takes a number as an argument and returns an array of that length (length enforced by the type system). You can partly get there with tuple types but you have to enumerate every length explicitly. And it only works if you pass a number literal to the function. If you pass a variable of type number, it can’t infer the particular number it is.


I just tried this, and I couldn't quite get it to work.

``` type PickType<B extends Boolean> = B extends true ? number ? string;

function foo<T extends Boolean>(t: T): PickType<T> { return t ? 42 : "42; } ```

The compiler doesn't seem to get that the type of T is narrowed in the ternary branches, and thus Pick<T> is determined.

The error I get is that a number isn't assignable to a Pick<T>.




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

Search: