4 ms·
Ah someone else posted a link and I understand the unsoundness now. The only time an issue ever came up for me was in dealing with arrays let foo: number[]
by kcrwfrd_ 2y ago
Ah someone else posted a link and I understand the unsoundness now.
The only time an issue ever came up for me was in dealing with arrays
let foo: number[] = [0, 1, 2]
// typed as number but it’s really undefined
let bar = foo[3]
But once you’re aware of the caveat it’s something you can deal with, and it certainly doesn’t negate the many massive benefits that TS confers over vanilla JS.
- lolinder 2y agoYeah, that example is unsound in the same way that Java's type system is unsound, it's a compromise nearly all languages make to avoid forcing you to add checks when you know what you're doing. That's not the kind of problem that people usually are referring to when they single out TypeScript.
- AfterAnimator 2y agoFor this case, I've switched to using `foo.at(3)` now instead, as it returns `T | undefined`, so you have to handle the undefined case.