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

how to force a field to be a specific type? #82

Closed
benny-medflyt opened this issue Jan 27, 2017 · 8 comments
Closed

how to force a field to be a specific type? #82

benny-medflyt opened this issue Jan 27, 2017 · 8 comments

Comments

@benny-medflyt
Copy link
Contributor

I am trying to do something like:

/**
* How to force a Widget to be encoded as number?
*
* @type number
*/
export interface Widget {}

export interface House {
    name: string;

    mainWidget: Widget;
    otherWidgets: Widget[];
}

So that the mainWidget field is encoded as a regular "number", and also otherWidgets will be an array of numbers.

In other words I would like to get the following schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "name": {
            "type": "string"
        },
        "mainWidget": {
            "type": "number"
        },
        "otherWidgets": {
            "items": {
                "type": "number"
            },
            "type": "array"
        }
    },
    "required": [
        "name",
        "mainWidget",
        "otherWidgets"
    ],
    "type": "object"
}

thank you

@domoritz
Copy link
Collaborator

Yes, you have to use:

/** @TJS-type integer */

See microsoft/TypeScript#13498 for the reason why this is necessary.

@benny-medflyt
Copy link
Contributor Author

Hi, I tried using @TJS-type but I don't think it helps me in this case

@domoritz
Copy link
Collaborator

Can you send a pull request with a test case? Thanks!

@benny-medflyt
Copy link
Contributor Author

Sent as #83

@domoritz
Copy link
Collaborator

The issue is that we try to infer types and defintions and overriding properties can have weird effects. I added some code so it works for types but I'm not convinced that it's a good idea in general to let users override properties.

@benny-medflyt
Copy link
Contributor Author

Thanks, this is amazing! But i have found a small bug i think when importing type from another module. I've added another test case in #84

@domoritz
Copy link
Collaborator

Thanks @benny-medflyt! Do you think you could look into a fix for the issue?

@benny-medflyt
Copy link
Contributor Author

I'm not familiar with typescript-json-schema source code, but maybe in a few days I'll have time to try to figure this out

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