Why can't I extend "any" in Typescript?

I want to create a placeholder interface for classes using an external library. So I did:

export interface IDatabaseModel extends any { } 

I would prefer to do this and add methods later (for example, findAll ()), than marking all my classes as “any”, and you will have to manually search and replace “any” in hundreds of specific places with “IDatabaseModel” later.

But the compiler complains that it cannot find "any".

What am I doing wrong?

+6
source share
3 answers

The type any is a way to refuse type checking, so it makes no sense to use it in an environment where type checking is performed.

I would recommend adding your methods to the interface instead when you use them:

 export interface IDatabaseModel { findAll(): any; } 
+2
source

With an alias for any

I want to create a stub interface

Use an alias:

 type IDatabaseModel = any; 

Or with an index signature [Edit from August 2017]

With TypeScript 2.2 , access syntax for the obj.propName resource with index signature is allowed:

 interface AnyProperties { [prop: string]: any } type MyType = AnyProperties & { findAll(): string[] } let a: MyType a.findAll() // OK, with autocomplete a.abc = 12 // OK, but no autocomplete 
+5
source

"any" is a type and, unlike other basic types, does not have an interface. Given that this is a wildcard, I can’t imagine what an interface could be for “anyone”.

+1
source

All Articles