Skip to content

Commit 46bbf83

Browse files
authored
fix typechecking for unions of array types (microsoft#10945)
1 parent ae13658 commit 46bbf83

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎pxtcompiler/emitter/emitter.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,14 @@ namespace ts.pxtc {
657657
}
658658

659659
function isArrayType(t: Type) {
660+
if (t.flags & TypeFlags.Union && (t as UnionType).types) {
661+
for (const subtype of (t as UnionType).types) {
662+
if (!isArrayType(subtype)) {
663+
return false;
664+
}
665+
}
666+
return true;
667+
}
660668
if (!isObjectType(t)) {
661669
return false;
662670
}
@@ -701,7 +709,12 @@ namespace ts.pxtc {
701709

702710
function arrayElementType(t: Type, idx = -1): Type {
703711
if (isArrayType(t))
704-
return checkType((<TypeReference>t).typeArguments[0])
712+
if (t.flags & TypeFlags.Union) {
713+
return checkType(t.getNumberIndexType());
714+
}
715+
else {
716+
return checkType((<TypeReference>t).typeArguments[0])
717+
}
705718
return checker.getIndexTypeOfType(t, IndexKind.Number);
706719
}
707720

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let multidim = [[1, 3], ["one", "two"]]
2+
let a = multidim[0][0]
3+
let b = multidim[1][1]
4+
5+
assert(a === 1)
6+
assert(b === "two");

‎tests/compile-test/lang-test0/pxt.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
"49unicode.ts",
5555
"50indexedtypes.ts",
5656
"51exceptions.ts",
57+
"52parseint.ts",
58+
"53unionarraytypes.ts",
5759
"99final.ts"
5860
],
5961
"public": true,

0 commit comments

Comments
 (0)