This project is read-only.

Generics error

Topics: General
Aug 1, 2013 at 12:00 AM
We have a cache data structure which has a method which looks like this:
    public get(key: K, valueLoader?: () => V): V {
        var value = this._cache.get(key);

        // load and save the value if one was not found in the _cache
        if (value === undefined && valueLoader !== undefined) {
            value = valueLoader();
            this.set(key, value);

        return value;
This compiled in, but with the in-progress 0.9.1 compiler I'm seeing the following error on the valueLoader parameter's type: "Return type of exported function is using inaccessible module".

In case it matters, the context for this code looks like this (we compile to AMD modules):
import Map = require("common/map");

class Cache<K, V> {

    private _cache: Map<K, V>;
    private _maxSize: number;

    constructor(maxSize?: number, lru = false) {
        if (maxSize !== undefined && maxSize < 1) {
            throw new Error("maxSize must be positive.");

        this._cache = new Map(lru);
        this._maxSize = maxSize;


export = Cache;
Is this error message expected? I can provide the entire file and its dependency (map.ts) if necessary. Thanks for any assistance!
Aug 5, 2013 at 11:16 PM
ping - i see there is another thread on generics but i'm not sure if this one is related.
Aug 6, 2013 at 11:46 AM
Edited Aug 6, 2013 at 11:53 AM
Can you try
get(key: K, valueLoader?: { (): V; }): V
instead of
get(key: K, valueLoader?: () => V): V
as the method signature?
Aug 6, 2013 at 5:44 PM
Whoa, good call: that works fine. Do you know why that works but the other syntax doesn't? Is it a bug in the compiler? (or a breaking change in 0.9.1)
Aug 6, 2013 at 8:52 PM
One of my colleagues found another fix which is the one I think we'll go with:
public get(key: K, valueLoader?: () => Cache.V): V {
Aug 7, 2013 at 11:08 AM
Edited Aug 7, 2013 at 11:09 AM
I find that doing it via an interface works:
interface FuncReturning<V> { (): V; }
public get(key: K, valueLoader?: FuncReturning<V>): V { ... }
This must be a bug in 0.9.1 since it didn't show up in 0.9.01.