jecki / ts2python

Python-interoperability for Typescript-Interfaces
Apache License 2.0
42 stars 4 forks source link

Class decoration order #23

Open fydelity opened 1 month ago

fydelity commented 1 month ago

Am I the first one to face an issue with the order that the python classes are declared after the conversion?

Some of the classes are declared after they are already referenced. In TS, the order of declaration does not matter so the .ts files do not respect the order. I wonder what others has done to fix that?

jecki commented 1 month ago

ts2python puts type-references between single-quotation marks if the referenced type has not yet been declared. (This is, as I understand it, the standard way, forward or circular type-references are dealt with in Python.) Example:

class Range {
    start: Position;
    end: Position;
}

class Position {
    line: number;
    column: number;
}

becomes:

class Range(TypedDict, total=True):
    start: 'Position'
    end: 'Position'

class Position(TypedDict, total=True):
    line: float
    column: float

If this does not cover the cases you were thinking of, please give me an example.