I would really love to enjoy TypeScript, especially now that they've added HKTs, but I am constantly running into type errors when the types clearly match, leading to weird workaround code where the type of some key in some object is specified as: false | 'x' | 'y' | undefined Meaning passing in `'x'` should work just fine. But instead, it specializes it to just a string, then throws a type error. The workaround requ…
I second that, to a degree. I write mostly TypeScript and Rust these days and it still behaves unexpectedly sometimes. Example: for a service we wanted to port Rusts’ Result. https://gist.github.com/KenanSulayman/34a40daa3ebbd1e1bdf7c7... Notice the ‘as any’ and ‘_T!: T;’ et al. hacks to make it work. Other than that it’s completely a one to one port from the Rust core implementation.
As it is, Ok only depends on T and Err only depends on E, so why do you have them depend on both?
One problem is that TS does not want to merge unionized function signatures like this:
map: ((fn: (arg: "yep") => U) => Ok) | ((fn: (arg: any) => U) => Result)
This can be mostly fixed by removing arguments that you don't need.I couldn't figure out .map_or_else, but .map().or_else() works.
Admittedly, with this way you can get an output of Ok | Ok from some functions, but I don't know where that would be a problem (still fully type checked, you would just get the expected error further outside.
Here's a updated gist: https://gist.github.com/phiresky/621f8d8ed6eb00c9a0ddb47217e...