3

In TypeScript 1.5, I have the following interface in IFoo.ts:

// IFoo.ts
interface IFoo<T> {
    bar(): T;
}

And an implementation in FooString.ts:

// FooString.ts
export default {
    bar: () => 'Hello world'
}

How can the module FooString.ts declare the object literal it is exporting as an implementation of IFoo<sring>? Without a declaration, the implementation of the interface is not checked by the compiler, and losing compile-time checking of the FooString module would be problematic.

2 Answers 2

2

Casting in 1.5 will preserve the compile-time checking, so this will work:

export default <IFoo<string>> {
    bar: () => 'Hello world'
}
Sign up to request clarification or add additional context in comments.

Comments

-1
export var defaults:IFoo<string> = {
    bar: () => 'Hello world'
}

2 Comments

This is rather sparse. Can you add some text to explain this code / how it solves the OP's problem?
Unfortunately not the appropriate answer. Exporting a variable named defaults does nothing.

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.