From 9d2a2c96344011a9f3db5ee85dc2cbb61c059b83 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 6 Jun 2019 13:33:36 -0700 Subject: [PATCH] Add tests --- .../propertyAccess/propertyAccessWidening.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts diff --git a/tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts b/tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts new file mode 100644 index 00000000000..ddcad3f57c9 --- /dev/null +++ b/tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts @@ -0,0 +1,22 @@ +// @strict: true + +// Repro from #31762 + +function g1(headerNames: any) { + let t = [{ hasLineBreak: false, cells: [] }]; + const table = [{cells: headerNames }].concat(t); +} + +function g2(headerNames: any) { + let t = [{ hasLineBreak: false, cells: [] }]; + const table = [{cells: headerNames }]["concat"](t); +} + +// Object in property or element access is widened when target of assignment + +function foo(options?: { a: string, b: number }) { + let x1 = (options || {}).a; // Object type not widened + let x2 = (options || {})["a"]; // Object type not widened + (options || {}).a = 1; // Object type widened, error + (options || {})["a"] = 1; // Object type widened, error +}