arthurfiorette / prisma-json-types-generator

⚒️ Changes JsonValues to your custom typescript type.
https://npm.im/prisma-json-types-generator
MIT License
379 stars 22 forks source link

Type recognized but runtime parsing does not yield the type. #401

Closed pauldesmondparker closed 1 week ago

pauldesmondparker commented 1 week ago

Raising this issue as documentation, in case it helps someone else in the future.

My LSP was recognizing my prisma field as the type I declared, but at runtime it was still a string.

My relevant schema.prisma:

generator client {
  provider = "prisma-client-js"
}

generator json {
  provider = "prisma-json-types-generator"
  allowAny = true
}

// ...

model rfq {
  id          Int   @id @default(autoincrement())
  /// ![{lat: number; lng: number }]
  a_geo_point Json
}

This was producing the desired result in my LSP. image

However, due to a mistake whereby I was still seeding the database with records saved with output from JSON.stringify, the whole process seemed to be short-circuited for the tests (vitest) that I was running.

The resolution was to apply the {lat: 22.2344, lng: 103.2564} object directly to the geo_point field in the seed.

arthurfiorette commented 1 week ago

Hi! I'd love if you could open a PR to document it <3

pauldesmondparker commented 1 week ago

I'm not sure that it deserves documentation. This is a case of me abusing the usage of Prisma. Writes are done with objects that conform to Prisma.JsonValue, and typescript will emit an error if it does not. However, I was passing in a string (the stringified object) and that technically conforms (RFC7159).

This issue is just for anyone else who happens to implement this particular comedy of errors.