Re:Ask HN: TypeScript experts, is this a type checking issue?
by mikekchar on Sunday September 01, 2019 @08:06AM
(#20851834)
I don't know TS, but instead of alert(typeof y(5)) why not try alert(typeof y('a')) I would hope you get a compile error. Looking at the code, I would guess that the "any" means that it's essentially a parameterised type. In my ideal world, it wouldn't mean that you can put literally any type in there at any time -- just that you can put any type that will satisfy the type checker. Because you are using it p as a number, it satisfies the type checker.
Another thing you can try is to pass x in as a parameter to a function. I would expect that this would not compile -- because the compiler can't guarantee (without jumping through incredible hoops) that x is always going to use p as a number. If it does compile, try to pass 2 functions: one numerical and one that uses chars. If that compiles, then I think you have a legitimate error in the type checker. Otherwise I think it's just fine.
Another thing you can try is to pass x in as a parameter to a function. I would expect that this would not compile -- because the compiler can't guarantee (without jumping through incredible hoops) that x is always going to use p as a number. If it does compile, try to pass 2 functions: one numerical and one that uses chars. If that compiles, then I think you have a legitimate error in the type checker. Otherwise I think it's just fine.