mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-01 12:42:58 +08:00
feat(collection): New basic collection types.
Adds four basic collection types for better type enforcement on collections. Assortment: <key: number || string>, <value: any> Collection: <key: number || string>, <value: T> Dictionary: <key: string>, <value: any> Thesaurus: <key: string>, <value: T>
This commit is contained in:
94
collection/collection-tests.ts
Normal file
94
collection/collection-tests.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
/// <reference path="collection.d.ts" />
|
||||
|
||||
// there are various instances in these tests when the <any> 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<number> = {};
|
||||
|
||||
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'] = <any>'b'; // would have thrown compiler error
|
||||
|
||||
// collections also should not return values that aren't their type
|
||||
var b: string = <any>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 = <any>{0: 'a', 'b': 'b'}; // should have thrown compiler error
|
||||
var a: string = newDictionary[<any>0]; // should have thrown compiler error
|
||||
newDictionary[<any>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<string> = {};
|
||||
|
||||
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<string> = <any>{0: '0', '1': '1'}; // should have thrown compiler
|
||||
// error
|
||||
var zero: string = newThesaurus[<any>0]; // should have thrown compiler error
|
||||
newThesaurus[<any>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'] = <any>2; // would have thrown compiler error
|
||||
// expecting any other type will throw a compiler error
|
||||
var two: number = <any>thesaurus['two']; // would have thrown compiler error
|
||||
}
|
||||
34
collection/collection.d.ts
vendored
Normal file
34
collection/collection.d.ts
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
// Type definitions for collections
|
||||
// Project: https://github.com/ttowncompiled/DefinitelyTyped
|
||||
// Definitions by: Ian Riley <https://github.com/ttowncompiled>
|
||||
// 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<T> {
|
||||
[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<T> {
|
||||
[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[]
|
||||
Reference in New Issue
Block a user