Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/generic_dictionary_untyped_arrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// not own: groups() stores borrowed elements in a dictionary, which -mm=own refuses
// A generic function with a dictionary of its type parameter, called with an untyped arrow (TSLANG 230): typing the
// arrow made ObjectDictionary<T> where its methods' result types were not found, or erased them with the trial run
// ("'ts.Call' op result type mismatch" in ObjectDictionary.delete, "...constructor does not reference a valid function").
class P {
constructor(public name: string, public age: number) {}
toString() {
return this.name;
}
}

function count<T>(items: T[], key: (item: T) => string): { [key: string]: number } {
const out: { [key: string]: number } = {};
for (const it of items) {
const k = key(it);
out[k] = (out[k] ?? 0) + 1;
}

return out;
}

function groups<T>(items: T[], key: (item: T) => string): { [key: string]: T[] } {
const out: { [key: string]: T[] } = {};
for (const it of items) {
const k = key(it);
if (k in out) out[k].push(it);
else out[k] = [it];
}

return out;
}

function first<T>(items: T[], key: (item: T) => string): { [key: string]: T } {
const out: { [key: string]: T } = {};
out[key(items[0])] = items[0];
return out;
}

function main() {
const c = count(["a", "bb", "cc"], (s) => "" + s.length);
assert(c["2"] == 2 && c["1"] == 1, "count");
const g = groups([1, 2, 3, 4], (n) => n % 2 == 0 ? "e" : "o");
assert(g["e"].length == 2 && g["o"][1] == 3, "groups");
const ages = groups([new P("x", 1), new P("y", 2), new P("z", 1)], (p) => "" + p.age);
assert(ages["1"].length == 2 && ages["2"][0].name == "y", "groups of objects");
const f = first([5, 6], (n) => "k");
assert(f["k"] == 5, "first");
console.log("ALL DONE");
}
Loading