-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.isA() should be a type guard #387
Comments
To me this sounds like a useful and rather simple addition to the UI5 type signatures. The only unfortunate thing is the necessary redundancy in the call (type param + string param). But the benefit of the type guard for the after-following block looks greater than that drawback. |
@sap-sebelao What do you mean with "existing usage of isA in TS code would become invalid"? Existing uses seem to work as before even without the overload that re-adds the non-generics signature. Anything special you had in mind here? |
I checked again and you are right: I think I assumed TS will show the same error as if you do not provide a parameter when you are asserting a generic type, eg. It does not seem to be the case for functions however! My bad, I will edit the post to avoid confusion. |
No problem, I might have assumed the same. I mainly asked because internally the implementation is easier for changing than for adding (due to how we overlay TypeScript-specific info). |
Fixes SAP/ui5-typescript#387 Change-Id: I9af434c901b991dfe425f364e6e94997bc17bc6e
Fixed by SAP/openui5@481bc37 (both the static and the instance variant on sap.ui.base.Object) and will be in the 1.112 types. |
When using methods such as
byId
, which have a very generic return type, TS then throws errors unless there is a type assertion (this topic was brought up in #356)Using type assertions is not a great approach, as it does not ensure the type will be correct on runtime. In this case, using the built in
.isA
method can help:However, TS still throws errors, because it does not recognize
isA
as a type guard.This could be fixed by generics, which in this case would make sense in my opinion:
Usage:
In general, the expression
<variable> is <type>
in place of return type makes TS understand the method/function as a type guard.In this case, replacing the variable with
this
and a specific type with theT
generic makes TS understand that this method is checking that the type ofthis
isT
.Of course, it partially relies on the developer to provide the correct/matching types for both the Type and the Name argument (but since it is an actual runtime check, such errors should get caught in testing immediately).
I think this would greatly benefit the usage of TS with UI5 as it would eliminate most needs for explicit type assertions.
I tested the above with a few simple situations with classes, interfaces and inheritance, and found no issues with this so far, but of course there could be limitations when put into practice inside a larger framework like Open/SAPUI5.
Could you consider this addition?
The text was updated successfully, but these errors were encountered: