Files
scriptc/tests/corpus/2001-generic-methods-generic-class.ts
2026-07-22 18:08:04 -05:00

30 lines
857 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Generic methods ON generic classes: one compiled method instance per
// (receiver instantiation × method instantiation) — the method body lowers
// under the class instantiation's bindings merged with its own.
class Box<T> {
v: T;
constructor(v: T) {
this.v = v;
}
map<U>(f: (x: T) => U): Box<U> {
return new Box(f(this.v));
}
zip<U>(u: U): string {
return `${String(this.v)}~${String(u)}`;
}
}
const nb = new Box(10);
console.log(nb.map((n) => n * 2).v);
console.log(nb.map((n) => `n=${n}`).v);
console.log(nb.zip("s"));
console.log(nb.zip(false));
const sb = new Box("str");
console.log(sb.map((s) => s.length).v);
console.log(sb.zip(1));
// A generic method's instantiation can demand a NEW class instantiation
// (Box<boolean> exists only through this map) — the joint fixpoint.
console.log(nb.map((n) => n > 5).v);