Fix typescript generator params (#9524)

* Fix typescript generator params

* add TSOptionalType, TSRestType, add more test
This commit is contained in:
Tan Li Hau
2019-02-18 15:22:22 +08:00
committed by Nicolò Ribaudo
parent 5bb1bb080f
commit bbb4d7b6d7
4 changed files with 49 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
let union: number | null | undefined;
let intersection: number & string;
let precedence1: number | string & boolean;
let precedence2: number & string | boolean;
let precedence1: number | (string & boolean);
let precedence2: (number & string) | boolean;

View File

@@ -426,6 +426,40 @@ describe("programmatic generation", function() {
}).toThrow();
});
});
describe("typescript generate parantheses if necessary", function() {
it("wraps around union for array", () => {
const typeStatement = t.TSArrayType(
t.TSUnionType([
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
t.TSNullKeyword(),
]),
);
const output = generate(typeStatement).code;
expect(output).toBe("((number & boolean) | null)[]");
});
it("wraps around intersection for array", () => {
const typeStatement = t.TSArrayType(
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
);
const output = generate(typeStatement).code;
expect(output).toBe("(number & boolean)[]");
});
it("wraps around rest", () => {
const typeStatement = t.tsRestType(
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
);
const output = generate(typeStatement).code;
expect(output).toBe("...(number & boolean)");
});
it("wraps around optional type", () => {
const typeStatement = t.tsOptionalType(
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
);
const output = generate(typeStatement).code;
expect(output).toBe("(number & boolean)?");
});
});
});
describe("CodeGenerator", function() {