Skip to content

Commit

Permalink
feat: add support for never type
Browse files Browse the repository at this point in the history
  • Loading branch information
hmil committed Mar 10, 2022
1 parent a66d14b commit 7442740
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions factory/formatter.ts
Expand Up @@ -27,6 +27,7 @@ import { UnionTypeFormatter } from "../src/TypeFormatter/UnionTypeFormatter";
import { UnknownTypeFormatter } from "../src/TypeFormatter/UnknownTypeFormatter";
import { VoidTypeFormatter } from "../src/TypeFormatter/VoidTypeFormatter";
import { MutableTypeFormatter } from "../src/MutableTypeFormatter";
import { NeverTypeFormatter } from "../src/TypeFormatter/NeverTypeFormatter";

export type FormatterAugmentor = (
formatter: MutableTypeFormatter,
Expand Down Expand Up @@ -54,6 +55,7 @@ export function createFormatter(config: Config, augmentor?: FormatterAugmentor):
.addTypeFormatter(new UndefinedTypeFormatter())
.addTypeFormatter(new UnknownTypeFormatter())
.addTypeFormatter(new VoidTypeFormatter())
.addTypeFormatter(new NeverTypeFormatter())

.addTypeFormatter(new LiteralTypeFormatter())
.addTypeFormatter(new EnumTypeFormatter())
Expand Down
3 changes: 2 additions & 1 deletion src/NodeParser/NeverTypeNodeParser.ts
Expand Up @@ -2,12 +2,13 @@ import ts from "typescript";
import { Context } from "../NodeParser";
import { SubNodeParser } from "../SubNodeParser";
import { BaseType } from "../Type/BaseType";
import { NeverType } from "../Type/NeverType";

export class NeverTypeNodeParser implements SubNodeParser {
public supportsNode(node: ts.KeywordTypeNode): boolean {
return node.kind === ts.SyntaxKind.NeverKeyword;
}
public createType(node: ts.KeywordTypeNode, context: Context): BaseType | undefined {
return undefined;
return new NeverType();
}
}
7 changes: 7 additions & 0 deletions src/Type/NeverType.ts
@@ -0,0 +1,7 @@
import { BaseType } from "./BaseType";

export class NeverType extends BaseType {
public getId(): string {
return "never";
}
}
3 changes: 2 additions & 1 deletion src/Type/UnionType.ts
@@ -1,5 +1,6 @@
import { BaseType } from "./BaseType";
import { uniqueTypeArray } from "../Utils/uniqueTypeArray";
import { NeverType } from "./NeverType";

export class UnionType extends BaseType {
private readonly types: BaseType[];
Expand All @@ -10,7 +11,7 @@ export class UnionType extends BaseType {
types.reduce((flatTypes, type) => {
if (type instanceof UnionType) {
flatTypes.push(...type.getTypes());
} else if (type !== undefined) {
} else if (type !== undefined && !(type instanceof NeverType)) {
flatTypes.push(type);
}
return flatTypes;
Expand Down
16 changes: 16 additions & 0 deletions src/TypeFormatter/NeverTypeFormatter.ts
@@ -0,0 +1,16 @@
import { Definition } from "../Schema/Definition";
import { SubTypeFormatter } from "../SubTypeFormatter";
import { BaseType } from "../Type/BaseType";
import { NeverType } from "../Type/NeverType";

export class NeverTypeFormatter implements SubTypeFormatter {
public supportsType(type: NeverType): boolean {
return type instanceof NeverType;
}
public getDefinition(type: NeverType): Definition {
return { not: {} };
}
public getChildren(type: NeverType): BaseType[] {
return [];
}
}
3 changes: 3 additions & 0 deletions test/valid-data-other.test.ts
Expand Up @@ -65,6 +65,9 @@ describe("valid-data-other", () => {
it("undefined-union", assertValidSchema("undefined-union", "MyType"));
it("undefined-property", assertValidSchema("undefined-property", "MyType"));

it("never", assertValidSchema("never", "BasicNever"));
it("never-record", assertValidSchema("never-record", "Mapped"));

it("any-unknown", assertValidSchema("any-unknown", "MyObject"));

it("multiple-roots1", assertValidSchema("multiple-roots1"));
Expand Down
3 changes: 3 additions & 0 deletions test/valid-data/never-record/main.ts
@@ -0,0 +1,3 @@
// This form is recommended by the typescript-eslint rule "ban-types" to mean
// "an object with no properties"
export type Mapped = Record<string, never>;
12 changes: 12 additions & 0 deletions test/valid-data/never-record/schema.json
@@ -0,0 +1,12 @@
{
"$ref": "#/definitions/Mapped",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Mapped": {
"additionalProperties": {
"not": {}
},
"type": "object"
}
}
}
1 change: 1 addition & 0 deletions test/valid-data/never/main.ts
@@ -0,0 +1 @@
export type BasicNever = never;
9 changes: 9 additions & 0 deletions test/valid-data/never/schema.json
@@ -0,0 +1,9 @@
{
"$ref": "#/definitions/BasicNever",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"BasicNever": {
"not": {}
}
}
}

0 comments on commit 7442740

Please sign in to comment.