Skip to content

Commit ebbe4d7

Browse files
committed
[ts plugin] add typing.Mapping support
1 parent 1e15264 commit ebbe4d7

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

py_codegen/plugins/typescript/Converter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from py_codegen.type_extractor.nodes.FunctionFound import FunctionFound
1818
from py_codegen.type_extractor.nodes.ListFound import ListFound
1919
from py_codegen.type_extractor.nodes.LiteralFound import LiteralFound
20+
from py_codegen.type_extractor.nodes.MappingFound import MappingFound
2021
from py_codegen.type_extractor.nodes.NoneNode import NoneNode
2122
from py_codegen.type_extractor.nodes.TupleFound import TupleFound
2223
from py_codegen.type_extractor.nodes.TypeOR import TypeOR
@@ -78,8 +79,11 @@ def get_identifier(self, node: NodeType) -> str:
7879
return f"I{node.name}"
7980
if isinstance(node, TypeOR):
8081
return f"{self.get_identifier(node.a)} | {self.get_identifier(node.b)}"
82+
# FIXME: need to handle type(key) == int / float / etc.
8183
if isinstance(node, DictFound):
8284
return f"{{ [id: string]: {self.get_identifier(node.value)} }}"
85+
if isinstance(node, MappingFound):
86+
return f"{{ [id: string]: {self.get_identifier(node.value)} }}"
8387
if isinstance(node, ListFound):
8488
return f"{self.get_identifier(node.typ)}[]"
8589
if isinstance(node, TupleFound):

py_codegen/plugins/typescript/__tests__/generate_ts_definitions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from py_codegen.test_fixtures.func_return_none import func_return_nullable
1111
from py_codegen.test_fixtures.func_with_tuple import func_with_tuple
1212
from py_codegen.test_fixtures.func_with_literals import func_with_literals
13+
from py_codegen.test_fixtures.func_with_mapping import func_with_mapping
1314

1415
from py_codegen.type_extractor.type_extractor import TypeExtractor
1516

@@ -38,3 +39,4 @@ def __write_ts_definition__(name: str, definition_str: str):
3839
generate_and_write_ts_definition(func_return_nullable)
3940
generate_and_write_ts_definition(func_with_tuple)
4041
generate_and_write_ts_definition(func_with_literals)
42+
generate_and_write_ts_definition(func_with_mapping)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function func_with_mapping(
2+
input: { [id: string]: number },
3+
): { [id: string]: string }

0 commit comments

Comments
 (0)