Hacker News new | ask | show | jobs
by wwwigham 1917 days ago
Funny that the author mentions TypeScript, then complains that maintaining a system like this that "doesn't exist elsewhere" would be hard. TypeScript, in fact, does an analysis almost exactly like what is described in the article. Possibly even more expansive, since it also allows for user-defined assertions and guards, in addition to simple syntactic narrowing.
1 comments

TypeScript doesn't perform this conditional analysis:

This is a false-positive:

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMO...

> since it also allows for user-defined assertions and guards

Psalm allows user-defined type guards:

https://psalm.dev/docs/annotating_code/adding_assertions/

You're correct that this generates a false-positive! This is a really interesting feature. Would love to see more static analysers/compilers implement this.

You might be aware already, but the examples aren't the same (I know it illustrated the same idea tho) as on the Psalm post. It might make sense to align them with the PHP example so people don't get tripped up on that (esp if they're not too familiar with TS and PHP quirks)! Additionally `!a` and `a !== null` (or `!=`) aren't the same in JS/TS, so maybe change those in your example too, just to get your point across better.

BTW, Psalm seems really cool! I've not used PHP in a long time, but I always felt it lacks tooling, so it's great to see people are making progress in that area. Keep up the good work!

> You might be aware already, but the examples aren't the same

Yeah, because both TS and PHP have the same treatment of `!expr` I use the two interchangeably when demonstrating functionality, but the exactly comparable example is here:

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMO...

> BTW, Psalm seems really cool! I've not used PHP in a long time, but I always felt it lacks tooling, so it's great to see people are making progress in that area.

Yeah, it's not the only open-source static analysis tool for PHP – there's also https://phpstan.org, which is even more popular