-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73814db
commit 3890ce1
Showing
5 changed files
with
59 additions
and
69 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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) | ||
}) |
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,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>() | ||
}) |
This file was deleted.
Oops, something went wrong.