core icon indicating copy to clipboard operation
core copied to clipboard

Checks against interfaces in TypeScript

Open Jand42 opened this issue 8 years ago • 1 comments

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 isX functions.
  • [ ] 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.

Jand42 avatar Oct 13 '17 15:10 Jand42

Needs fixing: the is... function is currently missing for interfaces with the Name attribute.

Jand42 avatar Oct 30 '17 09:10 Jand42