Skip to content

Commit 94df9cf

Browse files
committed
fix: Emit single with Prisma type
1 parent 471c405 commit 94df9cf

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/handlers/emit-single.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import AwaitEventEmitter from 'await-event-emitter';
2+
import { partition } from 'lodash';
23
import { PropertyDeclarationStructure } from 'ts-morph';
34

45
import { DMMF } from '../types';
@@ -17,11 +18,12 @@ function classProperty(
1718
) {
1819
const { location, isList, propertyType } = eventArguments;
1920
if (['inputObjectTypes', 'outputObjectTypes'].includes(location) && !isList) {
20-
const types = propertyType.filter(t => t !== 'null');
21-
property.type = types.map(t => `InstanceType<typeof ${t}>`).join(' | ');
22-
if (types.length !== propertyType.length) {
23-
// If null was removed
24-
property.type += ' | null';
25-
}
21+
const [safeTypes, instanceofTypes] = partition(
22+
propertyType,
23+
t => t === 'null' || t.startsWith('Prisma.'),
24+
);
25+
const mappedInstanceofTypes = instanceofTypes.map(t => `InstanceType<typeof ${t}>`);
26+
27+
property.type = [...mappedInstanceofTypes, ...safeTypes].join(' | ');
2628
}
2729
}

src/test/generate.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,25 @@ describe('emit single', () => {
15311531
}));
15321532
expect(types).toHaveLength(0);
15331533
});
1534+
1535+
it('type started with Prisma should not be wrapped to instanceof', async () => {
1536+
const types = sourceFile
1537+
.getClasses()
1538+
.flatMap(c => c.getProperties().map(p => ({ c, p })))
1539+
.map(({ c, p }) => ({
1540+
c,
1541+
p,
1542+
propertyType: p.getStructure().type,
1543+
}))
1544+
.map(({ propertyType }) => String(propertyType))
1545+
.filter(
1546+
propertyType =>
1547+
propertyType.includes('Prisma.') &&
1548+
propertyType.startsWith('InstanceType<'),
1549+
);
1550+
1551+
expect(types).toHaveLength(0);
1552+
});
15341553
});
15351554

15361555
describe('emit single second gen', () => {

0 commit comments

Comments
 (0)