Skip to content

Commit a833e01

Browse files
authored
Allow transpiling template literals (#36)
* allow transpiling template literals * fix test case
1 parent 0b94eca commit a833e01

4 files changed

Lines changed: 8 additions & 2 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"build": "babel --extensions .ts --ignore '**/*.test.ts' ./src -d dist --source-maps",
4848
"test": "jest",
4949
"typecheck": "tsc -p . --noEmit",
50-
"prepack": "yarn build"
50+
"prepack": "yarn build",
51+
"typescript2python": "yarn --silent single src/index.ts"
5152
},
5253
"bin": {
5354
"typescript2python": "./dist/index.js"

src/parseInlineType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export const tryToParseInlineType = (
6262
// TODO: figure out why we reach this and replace with correct type definition
6363
return `object`;
6464
}
65+
} else if (type.flags & TypeFlags.TemplateLiteral) {
66+
// there is no way to represent template literals in Python,
67+
// so we fallback to string
68+
return "str";
6569
} else {
6670
// assume interface or object, we need to create a helper type
6771
if (!globalScope) {

src/parseTypeDefinition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ts from "typescript";
1+
import ts, { TypeFlags } from "typescript";
22
import { ParserState } from "./ParserState";
33
import { getDocumentationStringForDict, parseProperty, parsePropertyForDict } from "./parseProperty";
44
import { getDocumentationStringForType } from "./getDocumentationStringForType";

src/testing/basic.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe("transpiling basic types", () => {
6262
// without strict mode the `undefined` gets lost here
6363
"T = float",
6464
],
65+
["export type T = `a.b.${string}`", "T = str"],
6566
])("transpiles %p to %p", async (input, expected) => {
6667
const result = await transpileString(input);
6768
expect(result).toEqual(expected);

0 commit comments

Comments
 (0)