Skip to content

Commit

Permalink
use vitest typechecks
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej-dyck committed Jul 22, 2024
1 parent 73814db commit 3890ce1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 69 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
},
"devDependencies": {
"@eslint/js": "9.7.0",
"@type-challenges/utils": "0.1.1",
"@types/node": "20.14.11",
"eslint": "9.7.0",
"typescript": "5.5.3",
Expand Down
14 changes: 3 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions types/Branded.Age-Year.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, expectTypeOf, test } from 'vitest'
import { raise } from '../raise/index.js'
import { Branded } from './Branded.ts'

/** branded numbers */
type Age = Branded<number, 'Age'>
type Year = Branded<number, 'Year'>

const validAge = (age: number): Age | undefined =>
age >= 0 && age <= 125 ? age as Age : undefined

const birthYear = (age: Age, now: Date): Year =>
now.getFullYear() - age as Year // this is incorrect, but sufficient for demo purpose

test('Age is compile-time save', () => {
const someAge = validAge(5) ?? raise('invalid age')

expectTypeOf(someAge).toEqualTypeOf<Age>()
expectTypeOf(someAge).not.toEqualTypeOf<number>()
})

test('birthYear is compile-time safe', () => {
const fiveYears = validAge(5) ?? raise('invalid age')

expectTypeOf(birthYear).parameters.toMatchTypeOf<[Age, Date]>()

const twothousandnineteen = birthYear(fiveYears, new Date(2024, 4, 11))
expect(twothousandnineteen).toBe(2019)

expectTypeOf(twothousandnineteen).toEqualTypeOf<Year>()
expectTypeOf(twothousandnineteen).not.toEqualTypeOf<number>()
})

test('birthYear\'s argument is still just a number in JS', () => {
expect(
// @ts-expect-error argument must be Age
birthYear(5, new Date(2024, 4, 11))
).toBe(2019)
})
17 changes: 17 additions & 0 deletions types/Branded.Email.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expectTypeOf, test } from 'vitest'
import { raise } from '../raise/index.js'
import { Branded } from './Branded.js'

/** branded string */
type Email = Branded<string, 'Email'>

const emailRegex = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/ // a simplified regex
const validEmail = (email: string): Email | undefined =>
emailRegex.test(email) ? email as Email : undefined

test('Email is compile-time save', () => {
const someEmail = validEmail('[email protected]') ?? raise('invalid email')

expectTypeOf(someEmail).toEqualTypeOf<Email>()
expectTypeOf(someEmail).not.toEqualTypeOf<string>()
})
57 changes: 0 additions & 57 deletions types/Branded.test.ts

This file was deleted.

0 comments on commit 3890ce1

Please sign in to comment.