Skip to content

Commit

Permalink
downlevel readonly array & readonly tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
Frezc committed Mar 11, 2020
1 parent b811012 commit 60355a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions baselines/ts3.4/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export interface E {
b: number;
}
export type F = Omit<E, 'a'>;
export declare let readonlyArr: ReadonlyArray<string>;
export declare let readonlyTuple: Readonly<[string, number]>;
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ function doTransform(checker, k) {
])
]);
}
} else if (ts.isTypeOperatorNode(n) && n.operator === ts.SyntaxKind.ReadonlyKeyword) {
if (ts.isArrayTypeNode(n.type)) {
// let arr: readonly number[];
// =>
// let arr: ReadonlyArray<number>;
return ts.createTypeReferenceNode('ReadonlyArray', [transform(n.type.elementType)]);
} else if (ts.isTupleTypeNode(n.type)) {
// let tup: readonly [string, number];
// =>
// let tup: Readonly<[string, number]>;
return ts.createTypeReferenceNode('Readonly', [transform(n.type)]);
}
}
return ts.visitEachChild(n, transform, k);
};
Expand Down
5 changes: 4 additions & 1 deletion test/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ export interface E {
b: number;
}

export type F = Omit<E, 'a'>;
export type F = Omit<E, 'a'>;

export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [string, number];

0 comments on commit 60355a5

Please sign in to comment.