core
core copied to clipboard
Checks against interfaces in TypeScript
Part of #820
WebSharper 4.0 translates type checks against interfaces to checking if a property with the shortest name in the interface is on the object. For example x :? IDisposable translates to "Dispose" in x.
This is unsatisfactory for TypeScript, because the in operator does not create a type guard:
if ("Dispose" in x) x.Dispose()
still fails to type-check if x is not an any.
A solution would be is to create a a helper is... function for every WebSharper interface which returns a user-defined type guard.
function isIDisposable(x): x is IDisposable {
return "Dispose" in x;
}
Then this works:
if (isIDisposable(x)) x.Dispose();
- [x] Print and use
isXfunctions. - [ ] Add the isX names during name resolution to avoid clash with explicitly defined functions. If a function with the name isX does exists for an interface, check that it returns bool and use that instead of the default generated function.
Needs fixing: the is... function is currently missing for interfaces with the Name attribute.