SAP-samples / ui5-typescript-helloworld

Showcase of a TypeScript setup for developing UI5 applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Discussion] How to do the type casts? (`<Button> something` or `something as Button`)

akudev opened this issue · comments

In the last "UI5ers live" online session, feedback was that typecasting with as is bad style and whether this can be fixed.

There are two syntax variants:

  • <Button> something
  • something as Button

The latter was introduced because the former does not work within JSX syntax. In UI5 JS code both could be used and they are functionally equivalent.

My initial search for style guides has led to:

Overall, I was not able to find any evidence that casting with as is generally considered "bad style". In contrary: if at all, as seems to be slightly preferred.

Casting at all is of course something that should be better avoided, but cannot in all cases (as seen from the TypeScript compiler code itself), especially when there are existing APIs returning superclasses. The idea to make use of generics to ease this a little exists, but comes with drawbacks.

But the discussion is open for additional input.