8 ms·
TS' type system is insane in what it's capable of expressing. I've personally wrote a snake game using types alone (you see a board, snake and snacks inside you
by Byamarro 3y ago
TS' type system is insane in what it's capable of expressing. I've personally wrote a snake game using types alone (you see a board, snake and snacks inside your editor type popups).
Despite that, TS is still quite cumbersome to use due to its initial mission statements. I.e. `Object.keys(myObject)` doesn't return `keyof (typeof myObject)`, so if you'd do something like this:
`Object.keys(myObject).forEach(key => myObject[key])` it'll throw an error that key is not assignable to `myObject`. The reasons lie in design choices made in order to be able to gradually migrate an untyped JS codebase into a TS one. There's tons of issues like this, some of them are just completely absurd and make developers that try to achieve simple things throw their laptop out of the window :D
- spankalee 3y agoThis is an old, old issue, and it's not due to migration but due to inheritance. `<T>keys(o: T) => keyof T` is actually quite incorrect, because you can't guarentee that keys() doesn't return a value not in `keyof T`, a key could be from a subclass of T. I think TypeScript is doing the right thing here in not claiming something that's very easily violated.