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]);