From 9e80e53bbee22b3f6287d1c9b479bbf5ec85ee52 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Sun, 29 Sep 2024 23:23:17 -0300 Subject: [PATCH] fix: useType option #292 --- src/util/create-signature.ts | 2 +- test/schemas/use-type.prisma | 27 ++++++++ test/types/use-type.test-d.ts | 116 ++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 test/schemas/use-type.prisma create mode 100644 test/types/use-type.test-d.ts diff --git a/src/util/create-signature.ts b/src/util/create-signature.ts index 3febefb..ca53a7f 100644 --- a/src/util/create-signature.ts +++ b/src/util/create-signature.ts @@ -21,7 +21,7 @@ export function createType( // If we should use a type as global type map if (config.useType) { - return `${config.namespace}.${config.useType}['${JSON.stringify(type)}']`; + return `${config.namespace}.${config.useType}[${JSON.stringify(type)}]`; } // Just return the type diff --git a/test/schemas/use-type.prisma b/test/schemas/use-type.prisma new file mode 100644 index 0000000..8d5af8c --- /dev/null +++ b/test/schemas/use-type.prisma @@ -0,0 +1,27 @@ +generator client { + provider = "prisma-client-js" + output = "../target/use-type" +} + +generator json { + provider = "node ./index.js" + useType = "MyOwnType" +} + +datasource db { + provider = "postgresql" + url = "" +} + +model Model { + id Int @id @default(autoincrement()) + + /// [Simple] + simple Json + + /// [Optional] + optional Json? + + /// [List] + list Json[] +} diff --git a/test/types/use-type.test-d.ts b/test/types/use-type.test-d.ts new file mode 100644 index 0000000..39e0bbf --- /dev/null +++ b/test/types/use-type.test-d.ts @@ -0,0 +1,116 @@ +import { expectAssignable, expectNotAssignable } from 'tsd'; +import type { Model, UpdateManyInput } from '../target/use-type/index'; + +declare global { + export namespace PrismaJson { + export type MyOwnType = { + Simple: 1; + Optional: 2; + List: 3; + }; + } +} + +expectAssignable({ + id: 0, + simple: 1, + optional: 2, + list: [3] +}); + +expectAssignable({ + id: 0, + simple: 1, + optional: null, + list: [3] +}); + +expectAssignable({ + id: 0, + simple: 1, + optional: null, + list: [] +}); + +expectAssignable({ + id: 0, + simple: 1, + optional: 2, + list: [3, 3, 3] +}); + +expectAssignable>({ + push: 3 +}); + +expectAssignable>({ + push: [] +}); + +expectAssignable>({ + push: [3] +}); + +expectAssignable>({ + push: [3, 3, 3] +}); + +expectAssignable>({ + set: [] +}); + +expectAssignable>({ + set: [3] +}); + +expectAssignable>({ + set: [3, 3, 3] +}); + +expectNotAssignable({ + id: 0, + simple: '1', + optional: 2, + list: [3] +}); + +expectNotAssignable({ + id: 0, + simple: 1, + optional: '2', + list: [3] +}); + +expectNotAssignable({ + id: 0, + simple: 1, + optional: 'undefined', + list: 3 +}); + +expectNotAssignable({ + id: 0, + simple: 1, + optional: 2, + list: '3,3,3' +}); + +expectNotAssignable>({ + push: '3' +}); + +expectNotAssignable>({ + push: ['3'] +}); + +expectNotAssignable>({ + set: 3 +}); + +expectNotAssignable>({ + set: '3' +}); + +expectNotAssignable>({ + set: ['3,3,3'] +});