Skip to content

Commit 3d75525

Browse files
Fix type name conflicts when using generic interfaces (#4)
1 parent e9bc4a4 commit 3d75525

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This changelog documents the changes between release versions.
55
Changes to be included in the next upcoming release
66

77
- Add support for JSDoc descriptions from object types ([#3](https://github.com/hasura/ndc-nodejs-lambda/pull/3))
8+
- Fix type name conflicts when using generic interfaces ([#4](https://github.com/hasura/ndc-nodejs-lambda/pull/4))
89

910
## v0.11.0
1011
- Add support for parallel execution of readonly functions ([#2](https://github.com/hasura/ndc-nodejs-lambda/pull/2))

ndc-lambda-sdk/src/inference.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,18 +518,21 @@ function getObjectTypeInfo(tsType: ts.Type, typePath: TypePathSegment[], typeChe
518518
}
519519
// Interface type - this covers:
520520
// interface IThing { test: string }
521+
// type AliasedIThing = IThing (the alias is erased by the compiler)
521522
else if (tsutils.isObjectType(tsType) && tsutils.isObjectFlagSet(tsType, ts.ObjectFlags.Interface)) {
522523
return {
523-
generatedTypeName: tsType.getSymbol()?.name ?? generateTypeNameFromTypePath(typePath),
524+
generatedTypeName: typeChecker.typeToString(tsType),
524525
properties: getMembers(tsType.getProperties(), typeChecker),
525526
description,
526527
}
527528
}
528529
// Generic interface type - this covers:
529530
// interface IGenericThing<T> { data: T }
531+
// type AliasedIGenericThing<T> = IGenericThing<T>
532+
// type AliasedClosedIGenericThing = IGenericThing<string>
530533
else if (tsutils.isTypeReference(tsType) && tsutils.isObjectFlagSet(tsType.target, ts.ObjectFlags.Interface) && typeChecker.isArrayType(tsType) === false && tsType.getSymbol()?.getName() !== "Promise") {
531534
return {
532-
generatedTypeName: tsType.getSymbol()?.name ?? generateTypeNameFromTypePath(typePath),
535+
generatedTypeName: typeChecker.typeToString(tsType),
533536
properties: getMembers(tsType.getProperties(), typeChecker),
534537
description,
535538
}

ndc-lambda-sdk/test/inference/basic-inference/basic-inference.test.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,40 @@ describe("basic inference", function() {
311311
name: "IThing"
312312
}
313313
},
314+
{
315+
argumentName: "aliasedInterface",
316+
description: null,
317+
type: {
318+
type: "named",
319+
kind: "object",
320+
name: "IThing"
321+
}
322+
},
314323
{
315324
argumentName: "genericInterface",
316325
description: null,
317326
type: {
318327
type: "named",
319328
kind: "object",
320-
name: "IGenericThing"
329+
name: "IGenericThing<string>"
330+
}
331+
},
332+
{
333+
argumentName: "aliasedGenericInterface",
334+
description: null,
335+
type: {
336+
type: "named",
337+
kind: "object",
338+
name: "AliasedIGenericThing<number>"
339+
}
340+
},
341+
{
342+
argumentName: "aliasedClosedInterface",
343+
description: null,
344+
type: {
345+
type: "named",
346+
kind: "object",
347+
name: "AliasedClosedIGenericThing"
321348
}
322349
},
323350
{
@@ -421,7 +448,7 @@ describe("basic inference", function() {
421448
},
422449
],
423450
},
424-
"IGenericThing": {
451+
"IGenericThing<string>": {
425452
description: null,
426453
properties: [
427454
{
@@ -435,6 +462,34 @@ describe("basic inference", function() {
435462
},
436463
],
437464
},
465+
"AliasedIGenericThing<number>": {
466+
description: null,
467+
properties: [
468+
{
469+
propertyName: "data",
470+
description: null,
471+
type: {
472+
name: "Float",
473+
kind: "scalar",
474+
type: "named",
475+
},
476+
},
477+
],
478+
},
479+
"AliasedClosedIGenericThing": {
480+
description: null,
481+
properties: [
482+
{
483+
propertyName: "data",
484+
description: null,
485+
type: {
486+
name: "Float",
487+
kind: "scalar",
488+
type: "named",
489+
},
490+
},
491+
],
492+
},
438493
"IThing": {
439494
description: null,
440495
properties: [

ndc-lambda-sdk/test/inference/basic-inference/complex-types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,25 @@ interface IThing {
1010
prop: string
1111
}
1212

13+
// This alias just gets erased by the TS compiler
14+
type AliasedIThing = IThing;
15+
1316
interface IGenericThing<T> {
1417
data: T
1518
}
1619

20+
type AliasedIGenericThing<T> = IGenericThing<T>
21+
22+
type AliasedClosedIGenericThing = IGenericThing<number>
23+
1724
type IntersectionObject = { wow: string } & Bar
1825

1926
type GenericIntersectionObject<T> = { data: T } & Bar
2027

28+
// This alias just gets erased by the TS compiler
2129
type AliasedString = string;
2230

31+
// These aliases just get erased by the TS compiler
2332
type GenericScalar<T> = GenericScalar2<T>
2433
type GenericScalar2<T> = T
2534

@@ -33,7 +42,10 @@ export function bar(
3342
genericAliasedObj: GenericBar<string>,
3443
genericAliasedObjWithComplexTypeParam: GenericBar<Bar>,
3544
interfce: IThing,
45+
aliasedInterface: AliasedIThing,
3646
genericInterface: IGenericThing<string>,
47+
aliasedGenericInterface: AliasedIGenericThing<number>,
48+
aliasedClosedInterface: AliasedClosedIGenericThing,
3749
aliasedIntersectionObj: IntersectionObject,
3850
anonIntersectionObj: {num:number} & Bar,
3951
genericIntersectionObj: GenericIntersectionObject<string>,

ndc-lambda-sdk/test/inference/type-descriptions/type-descriptions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe("type descriptions", function() {
7777
argumentName: "genericInterface",
7878
description: null,
7979
type: {
80-
name: "IGenericInterface",
80+
name: "IGenericInterface<string>",
8181
kind: "object",
8282
type: "named",
8383
}
@@ -131,7 +131,7 @@ describe("type descriptions", function() {
131131
}
132132
]
133133
},
134-
"IGenericInterface": {
134+
"IGenericInterface<string>": {
135135
description: "The most generic of interfaces",
136136
properties: [
137137
{

0 commit comments

Comments
 (0)