-
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 cb3cf21
Showing
5 changed files
with
70 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
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-ignore unused | ||
Check failure on line 33 in types/Branded.test.ts GitHub Actions / build
Check failure on line 33 in types/Branded.test.ts GitHub Actions / build
|
||
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 = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ | ||
Check failure on line 46 in types/Branded.test.ts GitHub Actions / build
|
||
const validEmail = (email: string): Email | undefined => | ||
emailRegex.test(email) ? email as Email : undefined | ||
|
||
const someEmail = validEmail('[email protected]') ?? raise('invalid email') | ||
|
||
// @ts-ignore unused | ||
Check failure on line 52 in types/Branded.test.ts GitHub Actions / build
Check failure on line 52 in types/Branded.test.ts GitHub Actions / build
|
||
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,3 @@ | ||
declare const __brand: unique symbol | ||
type Brand<B> = { [__brand]: B } | ||
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' |