Skip to content

Commit 030bb2a

Browse files
authored
Fix missing index increment (#1606)
1 parent 2963b01 commit 030bb2a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/transformation/utils/typescript/types.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,27 @@ export function isNumberType(context: TransformationContext, type: ts.Type): boo
5656

5757
function isExplicitArrayType(context: TransformationContext, type: ts.Type): boolean {
5858
if (context.checker.isArrayType(type) || context.checker.isTupleType(type)) return true;
59+
60+
if (type.isUnionOrIntersection()) {
61+
if (type.types.some(t => isExplicitArrayType(context, t))) {
62+
return true;
63+
}
64+
}
65+
66+
const baseTypes = type.getBaseTypes();
67+
if (baseTypes) {
68+
if (baseTypes.some(t => isExplicitArrayType(context, t))) {
69+
return true;
70+
}
71+
}
72+
5973
if (type.symbol) {
6074
const baseConstraint = context.checker.getBaseConstraintOfType(type);
6175
if (baseConstraint && baseConstraint !== type) {
6276
return isExplicitArrayType(context, baseConstraint);
6377
}
6478
}
6579

66-
if (type.isUnionOrIntersection()) {
67-
return type.types.some(t => isExplicitArrayType(context, t));
68-
}
69-
7080
return false;
7181
}
7282

test/unit/builtins/array.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,3 +884,17 @@ describe("copying array methods", () => {
884884
});
885885
});
886886
});
887+
888+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1605
889+
test("array indexing in optional chain (#1605)", () => {
890+
util.testModule`
891+
interface Foo extends Array<number> {}
892+
const b: {arr?: Foo} = {arr:[1,2]};
893+
export const t = b.arr?.[1];
894+
895+
const c: Foo | undefined = b.arr;
896+
export const u = c?.[1];
897+
`
898+
.setOptions({ strict: true }) // crucial to reproducing for some reason
899+
.expectToMatchJsResult();
900+
});

0 commit comments

Comments
 (0)