Reputation: 15113
There is a small, ad-hoc script I'm forced to require()
(not import
) and I want to wrap it in a typescript file with an adjacent declare class
statement.
declare class MyCache<K, T> {
constructor(numItems: number);
get(key: K): T;
set(key: K, val: T): void;
}
const Cache = require('./my-cache-module');
export default Cache as MyCache<K, T>; // <---- not quite right...
// intended to be used as:
//
// const cache: MyCache<string, string> = new MyCache(100);
// cache.set('foo', 'bar');
// cache.get('foo'); //-> 'bar'
How do I tell Typescript that Cache
is not an instance of MyCache
, but is instead the actual implementation of the class?
Upvotes: 0
Views: 42
Reputation: 250106
You need to type assert to typeof MyClass
instead. The class name is the instance type and typeof Class
is the type for the class itself.
export default cache as typeof MyCache;
Upvotes: 1