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

typescript2python

v2.0.5

Published

## About

Downloads

148

Readme

TypeScript2Python

About

This project implements a transpiler for creating pyright compatible type declarations automatically from TypeScript code! This is useful in a number of scenarios. For example:

  • Automatic generation of type-safe APIs between Node.js and Python services
  • Safely use JSON objects created by TypeScript projects in Python
  • A nice way to write complex Python types using TypeScript

Example

TypeScript

export type Foo = {
    type: "foo"
    foo: number[]
    optional?: string
}

/** DocStrings are supported! */
export type Bar = {
    type: "bar"
    bar: string
    /** nested objects need extra declarations in Python */
    nested: { 
        foo: Foo
    }
}

export type FooBarMap = { 
    [key: string]: Foo | Bar
}

export type TupleType = [string, Foo | Bar, any[]]

TypeScript2Python

from typing_extensions import Any, Dict, List, Literal, NotRequired, Tuple, TypedDict, Union

class Foo(TypedDict):
  type: Literal["foo"]
  foo: List[float]
  optional: NotRequired[str]

class Ts2Py_tliGTOBrDv(TypedDict):
  foo: Foo

class Bar(TypedDict):
  """
  DocStrings are supported!
  """
  type: Literal["bar"]
  bar: str
  nested: Ts2Py_tliGTOBrDv
  """
  nested objects need extra declarations in Python
  """

FooBarMap = Dict[str,Union[Foo,Bar]]

TupleType = Tuple[str,Union[Foo,Bar],List[Any]]

Usage

The easiest way to use TypeScript2Python, is to invoke it directly using npx and pointing it to one (or multiple) source files that export type declarations.

npx typescript2python <path to typescript source>

It will then output the transpiled code to the terminal. To save the output as a python file, simply pipe the result to the desired destination. For example npx typescript2python types.ts > types.py.

Features

TypeScript2Python supports many of TypeScripts type constructs, including:

  • Basic types, like boolean, number, string, undefined
  • Literal types, e.g. type X = 42, type Y = 'test'
  • Object types, { foo: string }
  • Unions, string | number
  • Arrays, boolean[]
  • Nested objects { bar: { foo: string } }, that will get transpiled into helper dictionaries
  • Optional properties { optional?: number }, that get transpiled to NotRequired[...] attributes
  • Docstrings /** this is very useful */

Transpiler options

Nullable optionals

In TypeScript objects, optional values can also be set to undefined. By default we assume the according Python type to be non-nullable, but a more closely matching behavior can be achieved using the flag --nullable-optionals. This will result in optional entries beeing transpiled as NotRequired[Optional[T]] instead of NotRequired[T].

Limitations

We currently do not support the following features:

  • Generics, as they cannot be fully supported by Python
  • Function signatures, as we restrict ourselves top serializable data
  • Values, as this is an extremely difficult problem and we currently only attempt to transpile types