-
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
611095e
commit a09daf5
Showing
6 changed files
with
91 additions
and
0 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
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,57 @@ | ||
import type { Equal, Expect, NotEqual } from '@type-challenges/utils' | ||
import { expect, 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 // it's incorrect, but sufficient for demo purpose | ||
|
||
test('birthYear is compile-time safe', () => { | ||
const fiveYears = validAge(5) ?? raise('invalid age') | ||
expect( | ||
birthYear(fiveYears, new Date(2024, 4, 11)) | ||
).toBe(2019) | ||
}) | ||
|
||
test('birthYear is compile-time safe', () => { | ||
expect( | ||
// @ts-expect-error argument must be Age | ||
birthYear(5, new Date(2024, 4, 11)) | ||
).toBe(2019) | ||
}) | ||
|
||
const someAge = validAge(5) ?? raise('invalid age') | ||
const someBirthYear = birthYear(someAge, new Date(2024, 4, 11)) | ||
|
||
// @ts-expect-error unused type as tests are compiler-based | ||
type AgeTests = [ | ||
Expect<Equal<typeof someAge, Age>>, | ||
Expect<NotEqual<typeof someAge, number>>, | ||
Expect<Equal<Parameters<typeof validAge>, [number]>>, | ||
Expect<Equal<Parameters<typeof birthYear>, [Age, Date]>>, | ||
Expect<Equal<typeof someBirthYear, Year>>, | ||
Expect<NotEqual<typeof someBirthYear, number>>, | ||
] | ||
|
||
/** 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 | ||
|
||
const someEmail = validEmail('[email protected]') ?? raise('invalid email') | ||
|
||
// @ts-expect-error unused type as tests are compiler-based | ||
type EmailTests = [ | ||
Expect<Equal<typeof someEmail, Email>>, | ||
Expect<NotEqual<typeof someEmail, string>>, | ||
Expect<Equal<Parameters<typeof validEmail>, [string]>>, | ||
] |
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,23 @@ | ||
declare const __brand: unique symbol | ||
type Brand<B> = { [__brand]: B } | ||
|
||
/** | ||
* A branded type for compile-time safe usage of (primitive) values. | ||
* | ||
* Example usage: | ||
* type Email = Branded<string, 'email'> | ||
* | ||
* const validEmail = (email: string): Email | undefined => | ||
* emailRegex.test(email) ? email as Email : undefined | ||
* | ||
* const subscribe = (email: Email) => { | ||
* ... | ||
* } | ||
* | ||
* ... | ||
* | ||
* ❌ subscribe('[email protected]') // ts-error | ||
* ✅ const email = validEmail('[email protected]') ?? raise('invalid email') | ||
* subscribe(email) // no ts-error | ||
*/ | ||
export type Branded<T, B> = T & Brand<B> |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './Branded.ts' | ||
export * from './DeepPartial.ts' |