3

typescript is shit.
I have never seen such a stupid bug in other languages.
https://stackblitz.com/edit/...

Apparently, there is no way to do type narrowing in a nested object without using enum

Comments
  • 3
    Well, it is a Microsoft language - so naturally it has some quirks. You can use an intermediate variable to help the compiler.
  • 0
    @Oktokolo Checked the latest version. The intermediate variable approach at the top will show as an error. The only walkaround now is to use Enum.

    https://codesandbox.io/s/...
  • 4
    I would assume (without checking) that it has more to do with the fact that typescript transpiles to JavaScript.

    Transpiling imposes certain kind of restrictions - as is mentioned many times thoughout the docs, Typescript **should** be as close to JS as possible.

    It isn't an independent language, it's just a lot of wrapping package around JS (aka the box full of excrements and other nasty stuff).

    I'd guess that the AST of TS is rather a "trivial" one, trivial meant in the way of being designed as an intermediary stage that only contains necessary information to transpile to JS. An AST in e.g. clang or other languages would be far more complex as it's more generic and has to cater with all the language specifics (language grammar, type system, etc.).
  • 0
    nvm. enum does not work.

    https://codesandbox.io/s/...
  • 0
    @Oktokolo Intermediate variable approach worked with enum case. Thanks
  • 1
  • 1
    @IntrusionCM Having decent type checking is the main selling point of TypeScript - I consider this a bug.

    But it can correctly identify the type when i use an intermediate variable. So i guess it can be attributed to the type checker not being mature enough - yet.
  • 1
    @Oktokolo Yes. Something similar. Thanks for the help. Posted a bug report on github
  • 1
    @h3rp1d3v Thanks for reporting the bug.
  • 2
    Shitscript with or without type checking that is hehe
  • 1
    well, typescript is just javascript with extra steps. what'd you expect?
  • 0
    Despite it being a MS product, it is open source. Have you considered raising an issue in Github?
Add Comment