Reputation: 720
Example of a typescript definition file using the decomposed class pattern:
declare module io {
interface IOStatic {
new(name: string): IOInstance;
name: string;
}
interface IOInstance {
get(): string
}
}
declare var IO: io.IOStatic;
declare module "IO" {
export = IO;
}
It is used in:
/// <reference path='external.d.ts' />
import IO = require('IO');
var x = new IO('John');
which works fine.
Question: how can I used the type definition of the IO instance in a type check, e.g.:
getName(io: IO): string {
return io.get();
}
error TS4022: Type reference cannot refer to container 'IO'. Should I export the instance definition too? If yes, how?
Upvotes: 1
Views: 628
Reputation: 250812
I'm not clear on what you're trying to do, so I'm sorry if this isn't answering your question correctly.
The types io.IOStatic
and io.IOInstance
are available to you to use like this:
import IO = require('IO');
var x = new IO('John');
function getName(io: io.IOInstance): string {
return io.get();
}
getName(x);
When you call new IO('John');
, you get an object of type io.IOInstance
.
Upvotes: 1