A library that converts Python dataclasses with type annotations to a TypeScript interface and serializes them to a file.
Project description
py-ts-interfaces
Python to TypeScript Interfaces
What is this?
This library provides utilities that convert Python dataclasses with type
annotations to a TypeScript interface
and serializes them to a file.
Installation
python --version # requires 3.7+
pip install py-ts-interfaces
Motivation
In web applications where Python is used in the backend and TypeScript is used
in the frontend, it is often the case that the client will make calls to the
backend to request some data with some specific pre-defined "shape". On the
client-side, an interface
for this data is usually defined and if the Python
backend authors use typechecking, like with mypy, the
project authors may be typing the JSON response values as well.
This results in a duplication of code. If the shape changes in the backend, the related interface must also be reflect its changes in the frontend. At best, this is annoying to maintain. At worst, over time the interfaces may diverge and cause bugs.
This library aims to have a single source of truth that describes the shape of the payload between the backend and the frontend.
Usage
In Python, py-ts-interfaces
exposes a new class object called Interface
.
By subclassing this object, you identify to the also-packaged script that you
want it to be serialized to an interface file.
- First, hook up your dataclasses:
# views.py
from dataclasses import dataclass
from py_ts_interfaces import Interface
@dataclass
class MyComponentProps(Interface):
name: str
show: bool
value: float
@dataclass
class WillNotGetPickedUp: # this doesn't subclass Interface, so it won't be included
name: str
value: float
- In your shell, run the included command and pass in the name of the file or directory you want to use. By default it will output to a file in your directory called interface.ts
$ py-ts-interface views.py
Created interface.ts!
You may also use the following arguments:
-o, --output [filepath]
: where the file will be saved. default isinterface.ts
.-a, --append
: by default each run will overwrite the output file. this flag allows only appends. Be warned, duplicate interfaces are not tested.
- The resulting file will look like this:
// interface.ts
interface MyComponentProps {
name: string;
show: boolean;
value: number;
}
Why @dataclass?
Dataclass
es were introduced in Python 3.7 and they are great. Some
alternatives that I have seen other codebases using are NamedTuple
and
TypedDict
. All of these objects attempt to do the same thing: group together
pieces of data that belong close together like a struct.
However, dataclass
won out over the other two for the following reasons:
- dataclasses are built-in to Python. As of writing,
NamedTuple
is also built-in to thetyping
module, butTypedDict
is still considered experimental. - dataclasses cannot be declared and defined inline like you can do with
NamedTuple
andTypedDict
, e.g.,NamedTuple
can be defined using class inheritance likeclass MyNamedTuple(NamedTuple): ...
, but also likeMyNamedTuple = NamedTuple('MyNamedTuple', [('name', str), ('id', int)])
. This is a good thing. Dataclasses require you to use a class style declaration, which not only looks closer to a TypeScript interface declaration, but it avoids the complex metaclass machinery that NamedTuples and TypedDicts use to gain all its features. Since this library uses the AST and static analysis of the code to determine what data to serialize, this makes the choice a no-brainer. - dataclasses can be made to be immutable (mostly) by setting
frozen=True
. This library does not require it but in later versions we may provide apartial
ed dataclass decorator that guarantees immutability. - Because we avoid the metaclass machinery of NamedTuples and TypedDicts, it
opens up the possibility of writing custom classes that allows
mypy
to typecheck it one way, but gives the AST parser some clues in order to generate TypeScript types that cannot easily be expressed in Python.
Why define the types in Python instead of TypeScript?
TypeScript is significantly more mature for typing syntax than Python. Generally speaking, you can express any type that Python can do in TypeScript, but not vice versa.
So defining the types in Python guarantee that you can also express the whole interface in both languages.
Supported Type Mappings
Please note that usage of T
U
and V
in the table below represent
stand-ins for actual types. They do not represent actually using generic typed
variables.
Python | Typescript |
---|---|
None | null |
str | string |
int | number |
float | number |
complex | number |
bool | boolean |
List | Array<any> |
Tuple | [any] |
List[T] | Array[T] |
Tuple[T, U] | [T, U] |
Optional[T] | T | null |
Union[T, U, V] | T | U | V |
Planned Supported Mappings
- String literals
- Undefined type
- isNaN type
- ReadOnly types
- Excess Properties
Unsupported/Rejected Mappings
The primary purpose of this library is to help type, first and foremost, data moving back and forth from client to server. Many of these features, whether they be specific to TypeScript or Python, would be overkill to support.
- void
- callables/functions
- enums
- Dates, datetime, dates, times (send these over as strings and convert them to richer objects on the client)
- extends
- generics, TypeVars
- intersection types
- mapped types
- conditional types
- classes
Author
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file py-ts-interfaces-0.1.0.tar.gz
.
File metadata
- Download URL: py-ts-interfaces-0.1.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.11 CPython/3.7.2 Linux/4.15.0-47-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0ec103e420d58675a4132141614bb57c7ca2b5d62e51e75fe317cc1d87fe87e |
|
MD5 | bb45d63ce85405c11f33979bd16dd803 |
|
BLAKE2b-256 | 5f57babff0291cf048beb39709cbf6575d42605673ece13f89bdecbacfc5663c |
File details
Details for the file py_ts_interfaces-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: py_ts_interfaces-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.11 CPython/3.7.2 Linux/4.15.0-47-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8520325dbf2199cbe09134cebee45c51120bfad976e153c1a0ef34a2493bd627 |
|
MD5 | 0cc7ff9e3d4f06e4a4db0b359a7cab3f |
|
BLAKE2b-256 | e01bfea011ab915404a7ea80b00a32599858fb6b8479ae0f9052faee4b95e8ac |