1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry the question isn't clear. However, your answer is right on spot. I did not know that the io variable was accessible like this. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.