Skip to content

Suggestion: implement "implements" operator #15174

Closed
@fis-cz

Description

@fis-cz

Hello,

I would like to ask if it would be possible to implement the "implements" operator for run-time returning true or false based on evaluation if object implements an interface.

I can imagine there will be a lot of edge cases but for simple class - implements statements it would be simply doable.

Lets take a look on the following example:

interface ICredentials {
   username: string;
   password: string;
}

class A implements ICredentials {
   username: string;
   password: string;
   ...
}

class B {
   ...
}

let a: A = new A();
let b: B = new B();

# true
console.log(A implements ICredentials);
console.log(a implements ICredentials);

# false
console.log(B implements ICredentials);
console.log(b implements ICredentials);

for such cases we need to write something like:

   function implementsICredentials(obj: any): boolean {
      return
         obj.hasOwnProperty("username") &&
         obj.hasOwnProperty("password");
   }

It is quiet a lot of code to be written manually if we want to test multiple interfaces. I can imagine the compiler could generate such functions for us.

Thanks for response.
Regards,
Franta

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions