Skip to content

Commit de2ea6e

Browse files
amake10xjs
andauthored
fix: generic index access constraint case (#542) (#2421)
* Add test for #542 * fix: generic index access constraint case (#542) --------- Co-authored-by: Neal Granger <[email protected]>
1 parent aae36b6 commit de2ea6e

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/NodeParser/IndexedAccessTypeNodeParser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ export class IndexedAccessTypeNodeParser implements SubNodeParser {
7979
return objectType;
8080
}
8181

82-
throw new LogicError(node, `Invalid index "${type.getValue()}" in type "${objectType.getId()}"`);
82+
// When the indexed property does not exist (e.g. constrained generics with
83+
// narrower instantiations), treat it as never so optional properties are dropped
84+
// instead of throwing.
85+
return new NeverType();
8386
}
8487

8588
throw new LogicError(node, `No additional properties in type "${objectType.getId()}"`);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { assertValidSchema } from "../../utils";
2+
import { test } from "node:test";
3+
4+
test("generic-indexed-access-constraint", assertValidSchema("generic-indexed-access-constraint", "MyObject"));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface MyGeneric<T extends { a?: string; b: number }> {
2+
a?: T["a"];
3+
b: T["b"];
4+
}
5+
6+
export interface MyObject extends MyGeneric<{ b: number }> {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$ref": "#/definitions/MyObject",
3+
"$schema": "http://json-schema.org/draft-07/schema#",
4+
"definitions": {
5+
"MyObject": {
6+
"additionalProperties": false,
7+
"properties": {
8+
"b": {
9+
"type": "number"
10+
}
11+
},
12+
"required": [
13+
"b"
14+
],
15+
"type": "object"
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)