Mohibeyki
Mohibeyki

Reputation: 459

use of any in typescript

I have an angular 1.4 project on typescript, the project is getting bigger and bigger and our team is really tired of interfaces that we have declared (so that all objects are typed, in comparison to any) I'm asking if this is a good idea or not? I chose typescript because I wanted to have a typed project, should I drop the interfaces or not?

Upvotes: 0

Views: 71

Answers (2)

Brocco
Brocco

Reputation: 64903

Interfaces can be very helpful if used properly.

If you are just doing this....

IFoo { ... }
Foo implements IFoo { ... }

Then they will not help as much as they do in other typed languages (C#/Java). Because the type checking in TypeScript is dependent upon the properties on the object and NOT on the declared type. This is because you can simply write this...

MyCtrl (foo: Foo) { ... }
//instead of 
MyCtrl {foo: IFoo) { ... }

This will not hinder unit testing in any way since as stated above the type checking is based upon properties and not declarations.

There are cases when interfaces can be quite helpful, for instance a common use case is when defining an object as a parameter...

doSomething (options: ISomethingOptions) { ... }

There is no harm in creating interfaces for everything, you just need to determine what level of typing works best for your team.

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115488

You can get by without using interfaces and going with any, but in the long term you are probably going to regret it. If your team is sick of the interfaces you've created, I would put time in fixing those instead of abandoning them. There is a significant argument around if typed languages reduce the number of errors found in code. Personally, I think they do.

What I've found with typed languages is that it helps remove stupid mistakes we all make, and this clears up our time to focus on actual logic problems in code. Not everyone agrees with me on this, but I will always pick a type language over a non typed one, especially if the team is used to dealing with languages like Java or C#.

Upvotes: 2

Related Questions