
Security News
Oxlint Introduces Type-Aware Linting Preview
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
A library that converts Python dataclasses with type annotations to a TypeScript interface and serializes them to a file.
This library provides utilities that convert Python dataclasses with type
annotations to a TypeScript interface
and serializes them to a file.
python --version # requires 3.7+
pip install py-ts-interfaces
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.
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.
# 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
$ py-ts-interfaces views.py
Created interface.ts!
You may also use the following arguments:
-o, --output [filepath]
: where the file will be saved. default is interface.ts
.-a, --append
: by default each run will overwrite the output file. this flag
allows only appends. Be warned, duplicate interfaces are not tested.// interface.ts
interface MyComponentProps {
name: string;
show: boolean;
value: number;
}
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:
NamedTuple
is also
built-in to the typing
module, but TypedDict
is still considered
experimental.NamedTuple
and TypedDict
, e.g., NamedTuple
can be defined using class
inheritance like class MyNamedTuple(NamedTuple): ...
, but also like
MyNamedTuple = 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.frozen=True
.
This library does not require it but in later versions we may provide a
partial
ed dataclass decorator that guarantees immutability.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.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.
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] |
Dict | Record<any, any> |
List[T] | Array[T] |
Tuple[T, U] | [T, U] |
Dict[T, U] | Record<T, U> |
Optional[T] | T | null |
Union[T, U, V] | T | U | V |
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.
Interested in contributing? You're awesome! It's not much, but here's some notes to get you started CONTRIBUTING.md.
FAQs
A library that converts Python dataclasses with type annotations to a TypeScript interface and serializes them to a file.
We found that py-ts-interfaces demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.