npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

python-typing-to-typescript

v1.1.0

Published

Convert python TypedDict's and type hints to typescript interface

Downloads

17

Readme

Note: This is in an experimental stage, currently only TypedDict's are handled, the interface equivalent in Python.

TypedDict's -> Interface

Say your API is hosted with Django or Flask, type hinting API responses with Python's TypedDict is then an only sane thing to do.

class Book(TypedDict):
    pages: int
    chapters: List[Chapter]
    authors: List[str]

class Chapter(TypedDict):
    title: str
    // short chapters only has one paragraph
    content: Union[str, List[Paragraph]]

class Paragraph(TypedDict):
    content: str

If your API consumer uses typescript, then rewriting this file as typescript interfaces is pretty much a necessity. This tool provides a CLI that does the conversion for you.

Our output:

export interface Book {
    pages: number;
    chapters: Chapter[];
    Authors: string[];
}
export interface Chapter {
    title: string;
    content: string | Paragraph[];
}
export interface Paragraph {
    content: string;
}

Installation

npm i -D python-typing-to-typescript

This creates CLI $ pttts (Python Typing To TypeScript)

Usage Examples

$ pttts schema.py schema.d.ts --python_interpreter venv/bin/python

$ pttts path/to/python/script.py path/to/output.ts

Do $ pttts -h to see details

Suggestion:

If you are a maintainer of your python API, consider having a schema.py file and set up automated @types/you-app npm package publish.

How

The program parses Python script with Python's built-in ast, and uses typescript's compiler APIs to transform the ast nodes.

Limitations

  • This is currently in an experimental stage. We mostly guaranteed it would work on our own schema.py. Some Python typing features are not coded in yet (to name a few: Any Dict dict Optional). Don't be sad! Most infrastructural work is already done in python_nodes.ts. It will be easy to support these features. Pull request / feature requests are welcome. See below in Typing Support section for what is supported.

  • You need to specify a python interpreter. It defaults to python3 if not provided. And We encourage using python 3.9. For now, this is intended to use with python 3.9. Over python versions, python ast specification has changed. What was an Ellipsis Node in python3.6 (dotdotdot notation), for example, is now a Constant Node. Different python versions need to be taken care of individually. We can support older/newer versions is there is a request.

  • The input file should only have TypedDicts classes. That is, no type aliases. Having extra code anywhere in the input file is OK, but code other than TypedDict classes will be ignored. For example if you use the below file as input, the first line User = string will be dropped.

    User = string
      
    class Book(TypedDict):
        owner: User
        price: int
        ...

    output:

    export interface Book{
      owner: User
      price: int
    }

Typing Support

Supported:

  • str, int, bool, tuple, list, ... (ellipsis notation)
  • Tuple, List, TypedDict
  • Literal, Union