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

Type aliases not getting referenced using --strictNullChecks with union types #291

Open
HoldYourWaffle opened this issue May 31, 2019 · 0 comments

Comments

@HoldYourWaffle
Copy link
Contributor

HoldYourWaffle commented May 31, 2019

Possibly related to #232.

Given the following TypeScript:

type NotNull = string;
type Null = null;

interface Type {
	neverNull: NotNull;
	alwaysNull: Null;
	nullable: NotNull | Null;
}

The following schema is generated using only --strictNullChecks:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "alwaysNull": { "type": "null" },
        "notnullable": { "type": "string" },
        "nullable": {
            "type": [ "null", "string" ]
        }
    },
    "type": "object"
}

So far so good. But when I add --aliasRefs I get the following:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "NotNull": {
            "type": "string"
        },
        "Null": {
            "type": "null"
        }
    },
    "properties": {
        "alwaysNull": { "$ref": "#/definitions/Null" },
        "notnullable": { "$ref": "#/definitions/NotNull" },
        "nullable": {
            "type": [
                "null",
                "string"
            ]
        }
    },
    "type": "object"
}

As you can see the nullable property doesn't reference the NotNull and Null types. This example is of course not a real-world scenario, but it's the easiest way to demonstrate the bug.

Interestingly, when I create a type Nullable = Null | NotNull and use that for a property the interface will reference the Nullable type, but the Nullable definition will not reference Null or NotNull:

type NotNull = string;
type Null = null;
type Nullable = Null | NotNull;

interface Type {
	nullableAliased: Nullable;
}
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "Nullable": {
            "type": [
                "null",
                "string"
            ]
        }
    },
    "properties": {
        "nullableAliased": {
            "$ref": "#/definitions/Nullable"
        }
    },
    "type": "object"
}
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

1 participant