Skip to content

Commit

Permalink
fix: useType option #292
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 30, 2024
1 parent c96937d commit 9e80e53
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/util/create-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/schemas/use-type.prisma
Original file line number Diff line number Diff line change
@@ -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[]
}
116 changes: 116 additions & 0 deletions test/types/use-type.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Model>({
id: 0,
simple: 1,
optional: 2,
list: [3]
});

expectAssignable<Model>({
id: 0,
simple: 1,
optional: null,
list: [3]
});

expectAssignable<Model>({
id: 0,
simple: 1,
optional: null,
list: []
});

expectAssignable<Model>({
id: 0,
simple: 1,
optional: 2,
list: [3, 3, 3]
});

expectAssignable<UpdateManyInput<Model['list'][number]>>({
push: 3
});

expectAssignable<UpdateManyInput<Model['list'][number]>>({
push: []
});

expectAssignable<UpdateManyInput<Model['list'][number]>>({
push: [3]
});

expectAssignable<UpdateManyInput<Model['list'][number]>>({
push: [3, 3, 3]
});

expectAssignable<UpdateManyInput<Model['list'][number]>>({
set: []
});

expectAssignable<UpdateManyInput<Model['list'][number]>>({
set: [3]
});

expectAssignable<UpdateManyInput<Model['list'][number]>>({
set: [3, 3, 3]
});

expectNotAssignable<Model>({
id: 0,
simple: '1',
optional: 2,
list: [3]
});

expectNotAssignable<Model>({
id: 0,
simple: 1,
optional: '2',
list: [3]
});

expectNotAssignable<Model>({
id: 0,
simple: 1,
optional: 'undefined',
list: 3
});

expectNotAssignable<Model>({
id: 0,
simple: 1,
optional: 2,
list: '3,3,3'
});

expectNotAssignable<UpdateManyInput<Model['list'][number]>>({
push: '3'
});

expectNotAssignable<UpdateManyInput<Model['list'][number]>>({
push: ['3']
});

expectNotAssignable<UpdateManyInput<Model['list'][number]>>({
set: 3
});

expectNotAssignable<UpdateManyInput<Model['list'][number]>>({
set: '3'
});

expectNotAssignable<UpdateManyInput<Model['list'][number]>>({
set: ['3,3,3']
});

0 comments on commit 9e80e53

Please sign in to comment.