Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get name of the interface as a string?

Tags:

Consider I have exported interface

export interface IMyAngularService{ } 

When we register service or factory we usually use function name as a name of registered service or simply derivable string (e.g. 'ISomeService' -> 'SomeService');

Later I can decide to rename interface and would like dependencies descriptions to change automatically:

class MyController{      static $inject = [         dependency(nameof<ISomeService1>),         dependency(nameof<ISomeService2>)     ];      constructor(...dependencies){     } } 

where dependency function conventionally gets service name from interface name. Thus whenever name of ISomeService1 is changed, resulting JavaScript contains changed strings.

like image 475
Pavel Voronin Avatar asked Jan 14 '15 14:01

Pavel Voronin


People also ask

How do you convert interface to string in Golang?

To convert interface to string in Go, use fmt. Sprint function, which gets the default string representation of any value. If you want to format an interface using a non-default format, use fmt. Sprintf with %v verb.

Can a class and interface have same name?

Case-3: When two interfaces contain a method with the same name, same signature but different return types, in this case, both interfaces can't be implemented in the same class. Separate classes need to be created to implement each interface of that type.

What is interface in TypeScript?

An Interface is a structure which acts as a contract in our application. It defines the syntax for classes to follow, means a class which implements an interface is bound to implement all its members.

Can an interface have a default value?

Since interfaces only work at compile-time, they cannot be used to set up runtime default values. Luckily, TypeScript provides a workaround for this. By using the TypeScript pick utility type, we can select properties from an interface and provide default values for them.


1 Answers

This is not possible. TypeScript doesn't generate any code for interfaces and there's nothing like the nameof operator.

like image 147
curpa Avatar answered Sep 21 '22 12:09

curpa