Skip to content

Commit

Permalink
test: branding example
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Oct 9, 2020
1 parent d1d85ab commit 61489a1
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions packages/typegen/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@ import {join} from 'path'
import {tmpdir} from 'os'
import {expectTypeOf} from 'expect-type'

type Brands = {
CountInfo: {
a_count: 'count'
}
}

type Branded<T, TypeName, Field> = TypeName extends keyof Brands
? Field extends keyof Brands[TypeName]
? T & {_brand: Brands[TypeName][Field]}
: T
: T

type OrigKnownTypes = typeof knownTypes

type WrappedKnownTypes = {
[TypeName in keyof OrigKnownTypes]: {
[Field in keyof OrigKnownTypes[TypeName]]: Branded<OrigKnownTypes[TypeName][Field], TypeName, Field>
}
}

describe('type generator', () => {
const writeTypes = join(__dirname, 'generated/main')
const {sql, poolConfig} = setupTypeGen({
reset: true,
knownTypes,
knownTypes: knownTypes as WrappedKnownTypes,
writeTypes,
})
const connectionString = `postgresql://postgres:postgres@localhost:5433/postgres`
Expand Down Expand Up @@ -47,11 +67,14 @@ describe('type generator', () => {
await slonik.query(sql.Foo`select * from foo`) // make sure duplicate doesn't create two types.

const countInfo = await slonik.one(sql.CountInfo`
select count(*) as a_count, a as a_value
from foo
group by a
`)
expectTypeOf(countInfo).toEqualTypeOf<{a_count: number; a_value: string}>()
select count(*) as a_count, a as a_value
from foo
group by a
`)
expectTypeOf(countInfo).toEqualTypeOf<{
a_count: number & {_brand: 'count'}
a_value: string
}>()

const generatedFiles = readdirSync(writeTypes)
generatedFiles.forEach(f => {
Expand Down

0 comments on commit 61489a1

Please sign in to comment.