Skip to content

Commit

Permalink
Fixed handling tuples when they are part of union/intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeniy Timokhov committed Mar 26, 2021
1 parent 4152e4d commit fe888b5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
return true;
}

if (objectType.objectFlags & ts.ObjectFlags.Reference) {
const target = (objectType as ts.TypeReference).target;
if (target !== objectType && isTypePropertyExternal(target, typePropertyName)) {
return true;
}
}

// in case when we can't get where a property come from in mapped types
// let's check the whole type explicitly
// thus in case of when property doesn't have a declaration let's treat any property of a mapped type as "external" if its parent type is external
Expand Down
15 changes: 15 additions & 0 deletions tests/test-cases/tuple-complex/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type TwoItems = [number, number];
type FourItems = [number, number, number, number];
type TupleOrNumber = number | TwoItems | FourItems;

function drawRoundRect(tupleOrNumber: TupleOrNumber): void {
if (!Array.isArray(tupleOrNumber)) {
console.log(tupleOrNumber.toExponential());
} else if (tupleOrNumber.length === 2) {
const [first, second] = tupleOrNumber;
console.log(first.toExponential(), second.toExponential());
} else if (tupleOrNumber.length === 4) {
const [first, second, third, fourth] = tupleOrNumber;
console.log(first.toExponential(), second.toExponential(), third.toExponential(), fourth.toExponential());
}
}
13 changes: 13 additions & 0 deletions tests/test-cases/tuple-complex/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function drawRoundRect(tupleOrNumber) {
if (!Array.isArray(tupleOrNumber)) {
console.log(tupleOrNumber.toExponential());
}
else if (tupleOrNumber.length === 2) {
var first = tupleOrNumber[0], second = tupleOrNumber[1];
console.log(first.toExponential(), second.toExponential());
}
else if (tupleOrNumber.length === 4) {
var first = tupleOrNumber[0], second = tupleOrNumber[1], third = tupleOrNumber[2], fourth = tupleOrNumber[3];
console.log(first.toExponential(), second.toExponential(), third.toExponential(), fourth.toExponential());
}
}

0 comments on commit fe888b5

Please sign in to comment.