Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/transformation/utils/typescript/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,27 @@ export function isNumberType(context: TransformationContext, type: ts.Type): boo

function isExplicitArrayType(context: TransformationContext, type: ts.Type): boolean {
if (context.checker.isArrayType(type) || context.checker.isTupleType(type)) return true;

if (type.isUnionOrIntersection()) {
if (type.types.some(t => isExplicitArrayType(context, t))) {
return true;
}
}

const baseTypes = type.getBaseTypes();
if (baseTypes) {
if (baseTypes.some(t => isExplicitArrayType(context, t))) {
return true;
}
}

if (type.symbol) {
const baseConstraint = context.checker.getBaseConstraintOfType(type);
if (baseConstraint && baseConstraint !== type) {
return isExplicitArrayType(context, baseConstraint);
}
}

if (type.isUnionOrIntersection()) {
return type.types.some(t => isExplicitArrayType(context, t));
}

return false;
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,17 @@ describe("copying array methods", () => {
});
});
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1605
test("array indexing in optional chain (#1605)", () => {
util.testModule`
interface Foo extends Array<number> {}
const b: {arr?: Foo} = {arr:[1,2]};
export const t = b.arr?.[1];

const c: Foo | undefined = b.arr;
export const u = c?.[1];
`
.setOptions({ strict: true }) // crucial to reproducing for some reason
.expectToMatchJsResult();
});