diff --git a/src/parseProperty.ts b/src/parseProperty.ts index 38492cf..6ee25ac 100644 --- a/src/parseProperty.ts +++ b/src/parseProperty.ts @@ -65,7 +65,9 @@ export const getDocumentationStringForDict = (state: ParserState, symbol: ts.Sym const documentation = symbol .getDocumentationComment(state.typechecker) .map((v) => v.text) + .filter(v => !!v) .join(" \n"); + if (documentation.length > 0) { return `${JSON.stringify(name)}: ${documentation}` } else { diff --git a/src/parseTypeDefinition.ts b/src/parseTypeDefinition.ts index c1d124d..85fe8ee 100644 --- a/src/parseTypeDefinition.ts +++ b/src/parseTypeDefinition.ts @@ -53,11 +53,12 @@ export const parseTypeDefinition = ( const propertyDocumentation = properties .map((v) => getDocumentationStringForDict(state, v)) - .filter(v => v !== undefined) + .filter(v => !!v) .join("\n"); - const innerDocstring = documentation?.replaceAll("\n", " \n") + (propertyDocumentation.length > 0 ? "\n## Entries\n" + propertyDocumentation : ""); + const innerDocstring = (documentation ?? "").replaceAll("\n", " \n") + (propertyDocumentation.length > 0 ? "\n## Entries\n" + propertyDocumentation : ""); const docstring = innerDocstring.length > 0 ? `\n"""\n${innerDocstring}\n"""` : ""; + const definition = `${name} = TypedDict(${JSON.stringify(name)}, {\n ${parsedProperties.join(",\n ")}\n})${docstring}`; state.statements.push(definition); diff --git a/src/testing/dicts.test.ts b/src/testing/dicts.test.ts index 77691b0..9cc918c 100644 --- a/src/testing/dicts.test.ts +++ b/src/testing/dicts.test.ts @@ -93,8 +93,10 @@ class A(TypedDict): const result = await transpileString(`export type A = { "foo.bar"?: string, }`); - expect(result).toContain( - `A = TypedDict("A", { + expect(result).toEqual( + `from typing_extensions import NotRequired, TypedDict + +A = TypedDict("A", { "foo.bar": NotRequired[str] })`, );