Typescript: Map,Set ... in ES5 target

Created on 28 May 2015  路  3Comments  路  Source: microsoft/TypeScript

After update the typescript compiler from version 1.4.1 to 1.5.0 beta, the source code included in my project became to make error log such as 'Map is not found'.
Thus, I researched this problem, and I reached an conclusion. The lib.d.ts became not to contain Map and Set definitions except ES6 being selected in tsconfig.json.

Actually, Map and Set is specification of ES6. It is natural that we can't use Map and Set in the project targeting ES5 or lower.
But we could use Map and Set even if I select ES5 as the target before typescript 1.4.1.
I know that making it being enabled to use Map and Set in the project that is made for ES5 contains risk.
Because there is some of browsers not supporting Map and Set.

However, I think it is better to make some of property of configuration that is for switching to use these definitions or not. Don't you think so?

Question

Most helpful comment

@LimeStreem we removed some IE-specific types from the library in 1.5.0-beta. you can just define the type in one of your files like:

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>;
    prototype: Map<any, any>;
}
interface Set<T> {
    add(value: T): Set<T>;
    clear(): void;
    delete(value: T): boolean;
    forEach(callbackfn: (value: T, index: T, set: Set<T>) => void, thisArg?: any): void;
    has(value: T): boolean;
    size: number;
}
declare var Set: {
    new <T>(): Set<T>;
    prototype: Set<any>;
}

All 3 comments

@LimeStreem we removed some IE-specific types from the library in 1.5.0-beta. you can just define the type in one of your files like:

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>;
    prototype: Map<any, any>;
}
interface Set<T> {
    add(value: T): Set<T>;
    clear(): void;
    delete(value: T): boolean;
    forEach(callbackfn: (value: T, index: T, set: Set<T>) => void, thisArg?: any): void;
    has(value: T): boolean;
    size: number;
}
declare var Set: {
    new <T>(): Set<T>;
    prototype: Set<any>;
}

Wow, I appreciate you to write this code snippet! :laughing:
I wonder this issue will help a lot of typescript users!

declare class WeakMap< Key , Value > {
    delete( key : Key ) : boolean
    get( key : Key ) : Value
    has( key : Key ) : boolean
    set( key : Key , value : Value ) : Map< Key , Value >
}

declare class Map< Key , Value > {
    clear(): void
    delete( key : Key ) : boolean
    forEach< Context = any >( handler : ( this : Context , value : Value , key : Key , map : Map< Key , Value > ) => void , context? : Context ) : void
    get( key : Key ) : Value
    has( key : Key ) : boolean
    set( key : Key , value : Value ) : Map< Key , Value >
    size : number
}

declare class Set< Value > {
    add( value : Value ) : Set< Value >
    clear() : void
    delete( value : Value ) : boolean
    forEach< Context = any >( handler : ( this : Context , value : Value , key : Value , map : Set< Value > ) => void , context? : Context ) : void
    has( value : Value ) : boolean
    size : number
}
Was this page helpful?
0 / 5 - 0 ratings