diff --git a/collection/collection-tests.ts b/collection/collection-tests.ts new file mode 100644 index 0000000000..6f81ca872a --- /dev/null +++ b/collection/collection-tests.ts @@ -0,0 +1,94 @@ +/// + +// there are various instances in these tests when the type +// is provided, this is to prevent the compiler from complaining +// when I violate the type enforcement posed by the various +// collection types. + +var assortment: Assortment = {} + +function testAssortment() { + // assortments can be indexed by either number or string + assortment[0] = 0; + assortment['1'] = 1; + + // assortments can store any type + assortment['a'] = 'a'; + assortment['true'] = true; +} + +testAssortment(); + + +var collection: Collection = {}; + +function testCollection() { + // collections can be indexed by either number or string + collection[0] = 0; + collection['1'] = 1; + + // a collection can always be expected to provide back its type + var zero: number = collection[0]; + var one: number = collection[1]; + + // collections should not store values that aren't of their type + collection['b'] = 'b'; // would have thrown compiler error + + // collections also should not return values that aren't their type + var b: string = collection['b']; // would have thrown compiler error +} + +testCollection(); + + +var dictionary: Dictionary = {}; + +function testDictionary() { + // dictionarys can store any type and are indexed by string + dictionary['b'] = 'b'; + dictionary['1'] = 1; + dictionary['true'] = true; + + // also, hopefully in the future, Dictionarys will error with these expressions as well + // However, TypeScript currently does not enforce key types for arrays + + // dictionarys should not use any index value, but string + var newDictionary: Dictionary = {0: 'a', 'b': 'b'}; // should have thrown compiler error + var a: string = newDictionary[0]; // should have thrown compiler error + newDictionary[1] = '1'; // should have thrown compiler error + + // dictionarys should be used with type checking because of this + if (typeof dictionary['b'] == 'string') { + var b_str: string = dictionary['b']; + } else if (typeof dictionary['b'] == 'number') { + var b_num: number = dictionary['b']; + } + + // but we can also just take the value if we don't care about the type + var one: any = dictionary['1']; +} + +testDictionary(); + + +var thesaurus: Thesaurus = {}; + +function testThesaurus() { + // a thesaurus is indexed by string + thesaurus['word'] = 'a combination of varters'; + + // it should not be indexed with any other value, but it can + // in the current build of TypeScript + var newThesaurus: Thesaurus = {0: '0', '1': '1'}; // should have thrown compiler + // error + var zero: string = newThesaurus[0]; // should have thrown compiler error + newThesaurus[1] = '1'; // should have thrown compile error + + // a thesaurus will return its respective value + var word: string = thesaurus['word']; + + // assigning any other type will throw a compiler error + thesaurus['two'] = 2; // would have thrown compiler error + // expecting any other type will throw a compiler error + var two: number = thesaurus['two']; // would have thrown compiler error +} \ No newline at end of file diff --git a/collection/collection.d.ts b/collection/collection.d.ts new file mode 100644 index 0000000000..8b203d554f --- /dev/null +++ b/collection/collection.d.ts @@ -0,0 +1,34 @@ +// Type definitions for collections +// Project: https://github.com/ttowncompiled/DefinitelyTyped +// Definitions by: Ian Riley +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +// an Assortment allows a variety of types, and each object can be referenced +// by number or by string. +interface Assortment { + [key: number]: any; + [key: string]: any; +} + +// a Collection only allows a single type, and each object can be referenced +// by number or by string. +interface Collection { + [key: number]: T; + [key: string]: T; +} + +// a Dictionary allows a variety of types, but each object must be referenced by string. +interface Dictionary { + [key: string]: any; +} + +// a Thesaurus only allows a single type, but each object must be referenced by string. +interface Thesaurus { + [key: string]: T; +} + +// an object that allows a variety of types, but each object is referenced by number +// is: any[] +// an object that allows only a single type, but each object is referenced by number +// is: T[]