Skip to content

Commit 785ca56

Browse files
authored
docs: add description for polymorphic ts typing (#315)
1 parent 9031453 commit 785ca56

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/guides/polymorphism.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,26 @@ model Video extends Content {
240240
}
241241
```
242242

243+
### Polymorphic TypeScript Types
244+
245+
The TS types generated by Prisma into the `@prisma/client` module have the database representation's typing and do not provide the "polymorphic view". For example, the following code doesn't compile:
246+
247+
```ts
248+
import type { Prisma } from '@prisma/client';
249+
250+
const where: Prisma.PostWhereInput = {};
251+
console.log(where.published); // error: Property 'published' does not exist on type 'PostWhereInput'
252+
```
253+
254+
To use the TS types with polymorphic view, import the `Prisma` type from the `@zenstackhq/runtime/models` module instead. This module contains types generated by ZenStack that comply with the ZModel schema.
255+
256+
```ts
257+
import type { Prisma } from '@zenstackhq/runtime/models';
258+
259+
const where: Prisma.PostWhereInput = {}
260+
console.log(where.published); // `PostWhereInput` has the `published` field
261+
```
262+
243263
### Sample Project
244264

245265
- Simple TypeScript script sample: [https://github.com/zenstackhq/v2-polymorphism](https://github.com/zenstackhq/v2-polymorphism)

0 commit comments

Comments
 (0)