From 00d77e82f35d9c1468779135ae081d2c14c1f90a Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 29 Nov 2026 18:46:36 +0000 Subject: [PATCH] Test: a generic function with a dictionary of T, called with untyped arrows (TSLANG 230) Needs the compiler of ASDAlexander77/TypeScriptCompiler#1069. Not own: groups() stores borrowed elements. Co-Authored-By: Claude Opus 5.5 --- tests/generic_dictionary_untyped_arrow.ts | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/generic_dictionary_untyped_arrow.ts diff --git a/tests/generic_dictionary_untyped_arrow.ts b/tests/generic_dictionary_untyped_arrow.ts new file mode 100644 index 0000000..f925bae --- /dev/null +++ b/tests/generic_dictionary_untyped_arrow.ts @@ -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 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(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(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(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"); +}