Skip to content

Fix mapped type additional properties when intersecting index signatures #12

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

Open
wants to merge 3 commits into
base: fix/additional-properties-crash-and-prop-loss
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/NodeParser/MappedTypeNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class MappedTypeNodeParser implements SubNodeParser {
return type instanceof NeverType ? new NeverType() : new ArrayType(type);
}
// Key type widens to `string`
const type = this.childNodeParser.createType(node.type!, context);
const type = this.childNodeParser.createType(node.type!, this.createSubContext(node, keyListType, context));
// const resultType = type instanceof NeverType ? new NeverType() : new ObjectType(id, [], [], type);
const resultType = new ObjectType(id, [], [], type);
if (resultType) {
Expand Down Expand Up @@ -162,13 +162,10 @@ export class MappedTypeNodeParser implements SubNodeParser {
return this.additionalProperties;
}

const key = keyListType.getTypes().filter((type) => !(derefType(type) instanceof LiteralType))[0];
const nonLiteral = keyListType.getTypes().find((type) => !(derefType(type) instanceof LiteralType));

if (key) {
return (
this.childNodeParser.createType(node.type!, this.createSubContext(node, key, context)) ??
this.additionalProperties
);
if (nonLiteral) {
return this.childNodeParser.createType(node.type!, this.createSubContext(node, nonLiteral, context));
}

return this.additionalProperties;
Expand Down
4 changes: 2 additions & 2 deletions src/NodeParser/TypeOperatorNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ts from "typescript";
import type { Context, NodeParser } from "../NodeParser.js";
import type { SubNodeParser } from "../SubNodeParser.js";
import { ArrayType } from "../Type/ArrayType.js";
import type { BaseType } from "../Type/BaseType.js";
import { BaseType } from "../Type/BaseType.js";
import { NumberType } from "../Type/NumberType.js";
import { ObjectType } from "../Type/ObjectType.js";
import { StringType } from "../Type/StringType.js";
Expand All @@ -28,7 +28,7 @@ export class TypeOperatorNodeParser implements SubNodeParser {
return new NumberType();
}
const keys = getTypeKeys(type);
if (derefed instanceof ObjectType && derefed.getAdditionalProperties()) {
if (derefed instanceof ObjectType && derefed.getAdditionalProperties() instanceof BaseType) {
return new UnionType([...keys, new StringType()]);
}

Expand Down
16 changes: 16 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,22 @@ describe("config", () => {
}),
);

it(
"mapped-intersection-index",
assertSchema("mapped-intersection-index", {
type: "MyObject",
additionalProperties: true,
}),
);

it(
"mapped-index-any",
assertSchema("mapped-index-any", {
type: "*",
additionalProperties: true,
}),
);

it(
"arrow-function-parameters",
assertSchema("arrow-function-parameters", {
Expand Down
15 changes: 15 additions & 0 deletions test/config/mapped-index-any/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface BaseA {
foo: string;
[key: string]: any;
}

type Keep<B> = { [K in keyof B]: B[K] };

export type MyObjectA = Keep<BaseA>;

interface BaseB {
foo: string;
[key: string]: unknown;
}

export type MyObjectB = { [K in keyof BaseB]: any };
25 changes: 25 additions & 0 deletions test/config/mapped-index-any/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObjectA": {
"properties": {
"foo": {
"type": "string"
}
},
"required": [
"foo"
],
"type": "object"
},
"MyObjectB": {
"properties": {
"foo": {}
},
"required": [
"foo"
],
"type": "object"
}
}
}
21 changes: 21 additions & 0 deletions test/config/mapped-intersection-complex/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"properties": {
"bar": {
"type": "number"
},
"foo": {
"type": "string"
}
},
"required": [
"bar",
"foo"
],
"type": "object"
}
}
}
11 changes: 11 additions & 0 deletions test/config/mapped-intersection-index/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface Base {
foo: string;
bar: null;
[key: string]: any;
}

type Keep<B, K> = { [P in Exclude<keyof B, K>]: B[P] };

type Override<B, O extends Partial<Record<keyof B, any>>> = Keep<B, keyof O> & O;

export type MyObject = Override<Base, { bar: number }>;
21 changes: 21 additions & 0 deletions test/config/mapped-intersection-index/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"properties": {
"bar": {
"type": "number"
},
"foo": {
"type": "string"
}
},
"required": [
"bar",
"foo"
],
"type": "object"
}
}
}