Deathspike
Deathspike

Reputation: 8770

TypeScript 1.5: Export default implementing an interface

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.

Upvotes: 3

Views: 1516

Answers (2)

Deathspike
Deathspike

Reputation: 8770

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

export default <IFoo<string>> {
    bar: () => 'Hello world'
}

Upvotes: 2

vasa_c
vasa_c

Reputation: 1004

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

Upvotes: -1

Related Questions