-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export enums and related types like Prisma does (#38)
* Export enums and related types like Prisma does * Update test name to better reflect the test * Add changeset * Append `as const` to enum objects * Fix tests after adding `as const` * Update changeset --------- Co-authored-by: Valtýr Örn Kjartansson <valtyr@gmail.com>
- Loading branch information
1 parent
49a110b
commit 2659cc3
Showing
6 changed files
with
84 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"prisma-kysely": patch | ||
--- | ||
|
||
Now using narrower types for enum objects bringing `prisma-kysely`'s enums in line with `prisma-client-js` (Thank you @jvandenaardweg 🎉) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import ts, { createPrinter } from "typescript"; | ||
import { expect, test } from "vitest"; | ||
|
||
import { generateEnumType } from "./generateEnumType"; | ||
|
||
test("it generates the enum type", () => { | ||
const [objectDeclaration, typeDeclaration] = generateEnumType("Name", [ | ||
{ name: "FOO", dbName: "FOO" }, | ||
{ name: "BAR", dbName: "BAR" }, | ||
]); | ||
|
||
const printer = createPrinter(); | ||
|
||
const result = printer.printList( | ||
ts.ListFormat.MultiLine, | ||
ts.factory.createNodeArray([objectDeclaration, typeDeclaration]), | ||
ts.createSourceFile("", "", ts.ScriptTarget.Latest) | ||
); | ||
|
||
expect(result).toEqual(`export const Name = { | ||
FOO: "FOO", | ||
BAR: "BAR" | ||
} as const; | ||
export type Name = (typeof Name)[keyof typeof Name];\n`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { stringifyTsNode } from "~/utils/testUtils"; | ||
|
||
import { generateTypedReferenceNode } from "./generateTypedReferenceNode"; | ||
|
||
test("it generated the typed reference node", () => { | ||
const node = generateTypedReferenceNode("Name"); | ||
|
||
const result = stringifyTsNode(node); | ||
|
||
expect(result).toEqual( | ||
"export type Name = (typeof Name)[keyof typeof Name];" | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import ts from "typescript"; | ||
|
||
export const generateTypedReferenceNode = (name: string) => { | ||
return ts.factory.createTypeAliasDeclaration( | ||
undefined, | ||
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], | ||
name, | ||
undefined, | ||
ts.factory.createTypeReferenceNode( | ||
`(typeof ${name})[keyof typeof ${name}]`, | ||
undefined | ||
) | ||
); | ||
}; |