dumbmatter
dumbmatter

Reputation: 9683

Allowed names for TypeScript interfaces

Consider this code, which compiles fine:

interface Whatever {
    name: string;
}

var x : Whatever = {
    name: "Whatever"
};

Change "Whatever" to "Map" and you get this code:

interface Map {
    name: string;
}

var x : Map = {
    name: "Whatever"
};

When I compile that using tsc (installed from npm on the latest Ubuntu), I get this nasty looking output:

test.ts(1,11): error TS2234: All declarations of an interface must have identical type parameters.
test.ts(5,5): error TS2012: Cannot convert '{ name: string; }' to 'Map<any, any>':
    Type '{ name: string; }' is missing property 'clear' from type 'Map<any, any>'.
test.ts(5,9): error TS2173: Generic type references must include all type arguments.

I'm completely new to TypeScript, so I'm not really sure what that means. I guess that something is already named Map by default, maybe? Anyone know what's going on? And is there some definitive list of restrictions on what I can name my interfaces?

Upvotes: 0

Views: 565

Answers (1)

WiredPrairie
WiredPrairie

Reputation: 59783

What you've encountered is that there are interfaces pre-defined for a number of ECMAScript 6 features, including Map (an explanation is here for the Map specification):

// lib.t.ts
//
/////////////////////////////
/// IE11 ECMAScript Extensions
/////////////////////////////
interface Map<K, V> {
    clear(): void;
    delete(key: K): boolean;
    forEach(callbackfn: (value: V, index: K, map: Map<K, V>) => void, thisArg?: any): void;
    get(key: K): V;
    has(key: K): boolean;
    set(key: K, value: V): Map<K, V>;
    size: number;
}
declare var Map: {
    new <K, V>(): Map<K, V>;
}

So, you just happened across a type that is already defined in the global namespace. If you wanted, you could add a module to allow you to use an exported interface without issue.

module Special {
    export interface Map {
        name: string;
    }
}

Upvotes: 2

Related Questions