|
| 1 | +import { transpileString } from "./utils"; |
| 2 | + |
| 3 | +describe("transpiling basic types", () => { |
| 4 | + it.each([ |
| 5 | + ["export type T = any;", "T = Any"], |
| 6 | + ["export type T = boolean;", "T = bool"], |
| 7 | + ["export type T = number;", "T = float"], |
| 8 | + ["export type T = string;", "T = str"], |
| 9 | + ["export type T = undefined;", "T = None"], |
| 10 | + ["export type T = void;", "T = None"], |
| 11 | + ["export type T = true;", "T = Literal[True]"], |
| 12 | + ["export type T = false;", "T = Literal[False]"], |
| 13 | + ["export type T = 42;", "T = Literal[42]"], |
| 14 | + ["export type T = 'foo';", 'T = Literal["foo"]'], |
| 15 | + ["export type T = {[key: string]: boolean};", "T = Dict[str,bool]"], |
| 16 | + ["export type T = {[key: string]: number};", "T = Dict[str,float]"], |
| 17 | + [ |
| 18 | + "export type T = {[key: string]: {[key: string]: number}};", |
| 19 | + "T = Dict[str,Dict[str,float]]", |
| 20 | + ], |
| 21 | + ["export type T = Record<string, number>;", "T = Dict[str,float]"], |
| 22 | + [ |
| 23 | + "export type T = number | string | Record<string, boolean>", |
| 24 | + "T = Union[str,float,Dict[str,bool]]", |
| 25 | + ], |
| 26 | + ])("transpiles %p to %p", async (input, expected) => { |
| 27 | + const result = await transpileString(input); |
| 28 | + expect(result.split("\n")[2]).toEqual(expected); |
| 29 | + }); |
| 30 | + |
| 31 | + it("only transpiles exported types", async () => { |
| 32 | + const result = await transpileString(` |
| 33 | + type NotExported = number; |
| 34 | + const notExported: NotExported = 42; |
| 35 | + export type Exported = number; |
| 36 | + export const exported: Exported = 42; |
| 37 | + `); |
| 38 | + expect(result).not.toContain("NotExported"); |
| 39 | + expect(result).not.toContain("exported"); |
| 40 | + expect(result).toContain("Exported = float"); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +export type T = Record<string, string>; |
0 commit comments