Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect TS type is generated when involving Tuple #11

Open
EonYang opened this issue May 24, 2021 · 1 comment
Open

Incorrect TS type is generated when involving Tuple #11

EonYang opened this issue May 24, 2021 · 1 comment

Comments

@EonYang
Copy link

EonYang commented May 24, 2021

To reproduce:

#types.py
from typing import List, Tuple
from pydantic import BaseModel


class Model(BaseModel):
    tuple: Tuple[str, int]
    list_of_tuples: List[Tuple[str, int]]
pydantic2ts --module ./types.py --output ./types.ts

yields:

//types.ts
export interface Model {
  tuple: [] | [string] | [string, number];
  list_of_tuples: [] | [string] | [string, number][];
}

Expected

The above Pydantic model should generate this:

export interface Model {
  tuple:  [string, number];
  list_of_tuples: [string, number][];
}

Versions:

python: 3.7.9
pydantic: 1.6 | 1.8.2
pydantic-to-typescript: 1.0.7

@EonYang EonYang changed the title Wrong TS type is generated when involving Tuple Incorrect TS type is generated when involving Tuple May 24, 2021
@phillipdupuis
Copy link
Owner

Hi, thanks for finding this. It's a bit of a funky one

It seems that the issue is resolved if the json schema has minItems: 2. If you run your test with this model, it passes

from typing import List, Tuple
from pydantic import BaseModel


class Model(BaseModel):
    tuple: Tuple[str, int]
    list_of_tuples: List[Tuple[str, int]]

    class Config:
        @staticmethod
        def schema_extra(schema: dict) -> None:
            """Customize the generated JSON schema via mutation"""
            schema["properties"]["tuple"]["minItems"] = 2
            schema["properties"]["list_of_tuples"]["items"]["minItems"] = 2

However, it appears that they already talked about adding this functionality into pydantic but decided against it:
pydantic/pydantic#718

We can certainly implement it ourselves by adding some behavior to the clean_schema function (https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/pydantic2ts/cli/script.py#L114) , just need to take care not to break anything else in doing so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants