From c503fe8b6b56fcff0c73884e5600fdd237a81c2a Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 28 Oct 2026 14:40:26 +0000 Subject: [PATCH] An optional parameter fits an overload as the type it holds `new Array(10)` against DefaultLib's Array constructors `(count: int = 0)` and `(...args: T[])` went to the rest constructor (length 1): overloadArgumentFit scored the defaulted parameter as optional, which an int reached only by conversion (1), against the rest element's exact match (3). The optional is now stripped before scoring, so both fit exactly and the one declared first wins. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenAccessCall.cpp | 5 +++++ .../tests/00class_constructor_overloads.ts | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp index 32cd64219..308a8e149 100644 --- a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp +++ b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp @@ -1892,6 +1892,11 @@ namespace mlirgen return 3; } + // an optional parameter (`count: int = 0`) takes what it holds as well as that: scored as + // optional, an int fitted only by conversion, and `new Array(10)` went to + // `(...args: T[])` declared after `(count: int)` + paramType = mth.stripOptionalType(paramType); + auto literalType = dyn_cast(argType); auto baseType = literalType ? literalType.getElementType() : argType; // `number` is a C `double`, as f64 is diff --git a/tslang/test/tester/tests/00class_constructor_overloads.ts b/tslang/test/tester/tests/00class_constructor_overloads.ts index 5630689c0..83d3d914d 100644 --- a/tslang/test/tester/tests/00class_constructor_overloads.ts +++ b/tslang/test/tester/tests/00class_constructor_overloads.ts @@ -39,7 +39,27 @@ class B { } } +// an optional parameter fits as the type it holds: `(count: int = 0)` takes an int as well as the +// rest parameter's elements do, and declared first it wins (it was scored as optional, by +// conversion, and `new C(10)` went to `(...args: T[])`) +class C { + n = 0; + + constructor(count: int = 0) { + this.n = count; + } + + constructor(...args: T[]) { + this.n = -args.length; + } +} + function main() { + const c1 = new C(10); + const c3 = new C(1, 2, 3); + assert(c1.n == 10); + assert(c3.n == -3); + const a0 = new A(); const a1 = new A(10); const a2 = new A([1, 2]);