While taking a look at what's new in TypeScript 2.0, I've found the never
type. According to the docs, it seems like it's a clever way of setting the type of functions that never return.
Now, if I read everything correctly, then never
can be assignable to every type, but only never
can be assigned to never
. So, while writing a small test in VS Code, I've ended up with the following:
function generateError(){
throw new Error("oops");
}
function do(option: "opt1" | "opt2"){
if(option === "opt1") return true;
if(option === "opt2 ) return false;
generateError();
}
let res = do("blah");
Well, what's the expected type of res
? According to the compiler, it's string | undefined
(which makes sense, though I must say I expected string
). I guess I'm not seeing the point of having a new type just to represent functions that never return. Do we really need this concept? IS this just a compiler thing that helps it have better flow analysis?
boolean