Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typegen): typescript: deprecate Json type in favour of unknown #750

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const apply = async ({
.forEach((c) => columnsByTableId[c.table_id].push(c))

let output = `
export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[]
/** @deprecated Use \`unknown\` instead. \`Json\` will be removed in a future release. */
export type Json = unknown

export type Database = {
${schemas
Expand Down Expand Up @@ -136,7 +137,12 @@ export type Database = {
return `${output}?: never`
}

output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })}`
output += `?: ${pgTypeToTsType(column.format, {
types,
schemas,
tables,
views,
})}`

if (column.is_nullable) {
output += '| null'
Expand Down Expand Up @@ -201,7 +207,12 @@ export type Database = {
return `${output}?: never`
}

output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })} | null`
output += `?: ${pgTypeToTsType(column.format, {
types,
schemas,
tables,
views,
})} | null`

return output
})}
Expand All @@ -214,7 +225,12 @@ export type Database = {
return `${output}?: never`
}

output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })} | null`
output += `?: ${pgTypeToTsType(column.format, {
types,
schemas,
tables,
views,
})} | null`

return output
})}
Expand Down Expand Up @@ -372,7 +388,12 @@ export type Database = {
const type = types.find(({ id }) => id === type_id)
let tsType = 'unknown'
if (type) {
tsType = `${pgTypeToTsType(type.name, { types, schemas, tables, views })} | null`
tsType = `${pgTypeToTsType(type.name, {
types,
schemas,
tables,
views,
})} | null`
}
return `${JSON.stringify(name)}: ${tsType}`
})}
Expand Down Expand Up @@ -509,7 +530,7 @@ const pgTypeToTsType = (
) {
return 'string'
} else if (['json', 'jsonb'].includes(pgType)) {
return 'Json'
return 'unknown'
} else if (pgType === 'void') {
return 'undefined'
} else if (pgType === 'record') {
Expand Down
42 changes: 16 additions & 26 deletions test/server/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import { app } from './utils'
test('typegen', async () => {
const { body } = await app.inject({ method: 'GET', path: '/generators/typescript' })
expect(body).toMatchInlineSnapshot(`
"export type Json =
| string
| number
| boolean
| null
| { [key: string]: Json | undefined }
| Json[]
"/** @deprecated Use \`unknown\` instead. \`Json\` will be removed in a future release. */
export type Json = unknown

export type Database = {
public: {
Expand Down Expand Up @@ -41,23 +36,23 @@ test('typegen', async () => {
category: number | null
created_at: string
id: number
metadata: Json | null
metadata: unknown | null
name: string
status: Database["public"]["Enums"]["meme_status"] | null
}
Insert: {
category?: number | null
created_at: string
id?: number
metadata?: Json | null
metadata?: unknown | null
name: string
status?: Database["public"]["Enums"]["meme_status"] | null
}
Update: {
category?: number | null
created_at?: string
id?: number
metadata?: Json | null
metadata?: unknown | null
name?: string
status?: Database["public"]["Enums"]["meme_status"] | null
}
Expand Down Expand Up @@ -183,19 +178,19 @@ test('typegen', async () => {
Row: {
created_at: string | null
id: number
previous_value: Json | null
previous_value: unknown | null
user_id: number | null
}
Insert: {
created_at?: string | null
id?: number
previous_value?: Json | null
previous_value?: unknown | null
user_id?: number | null
}
Update: {
created_at?: string | null
id?: number
previous_value?: Json | null
previous_value?: unknown | null
user_id?: number | null
}
Relationships: []
Expand Down Expand Up @@ -487,13 +482,8 @@ test('typegen w/ one-to-one relationships', async () => {
query: { detect_one_to_one_relationships: 'true' },
})
expect(body).toMatchInlineSnapshot(`
"export type Json =
| string
| number
| boolean
| null
| { [key: string]: Json | undefined }
| Json[]
"/** @deprecated Use \`unknown\` instead. \`Json\` will be removed in a future release. */
export type Json = unknown

export type Database = {
public: {
Expand Down Expand Up @@ -524,23 +514,23 @@ test('typegen w/ one-to-one relationships', async () => {
category: number | null
created_at: string
id: number
metadata: Json | null
metadata: unknown | null
name: string
status: Database["public"]["Enums"]["meme_status"] | null
}
Insert: {
category?: number | null
created_at: string
id?: number
metadata?: Json | null
metadata?: unknown | null
name: string
status?: Database["public"]["Enums"]["meme_status"] | null
}
Update: {
category?: number | null
created_at?: string
id?: number
metadata?: Json | null
metadata?: unknown | null
name?: string
status?: Database["public"]["Enums"]["meme_status"] | null
}
Expand Down Expand Up @@ -673,19 +663,19 @@ test('typegen w/ one-to-one relationships', async () => {
Row: {
created_at: string | null
id: number
previous_value: Json | null
previous_value: unknown | null
user_id: number | null
}
Insert: {
created_at?: string | null
id?: number
previous_value?: Json | null
previous_value?: unknown | null
user_id?: number | null
}
Update: {
created_at?: string | null
id?: number
previous_value?: Json | null
previous_value?: unknown | null
user_id?: number | null
}
Relationships: []
Expand Down