-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Type.Assert
HKT that implements behavior of TypeScript as
ass…
…ertions
- Loading branch information
Showing
3 changed files
with
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Test, Type } from '..' | ||
|
||
type Assert_Spec = [ | ||
/** | ||
* Can cast a type to itself. | ||
*/ | ||
Test.Expect<Type._$assert<true, true>, true>, | ||
|
||
/** | ||
* Can cast a type to a subtype. | ||
*/ | ||
Test.Expect<Type._$assert<boolean, true>, true>, | ||
|
||
/** | ||
* Can cast a type to a supertype. | ||
*/ | ||
Test.Expect<Type._$assert<true, boolean>, boolean>, | ||
|
||
/** | ||
* Returns `never` if the types are not related. | ||
*/ | ||
Test.Expect<Type._$assert<boolean, 0>, never> | ||
] |
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,43 @@ | ||
import { Kind } from '..' | ||
|
||
/** | ||
* `_$assert` is a generic type that casts a type `T` to a type `U`, but only if `U` is a narrower or wider version of `T`. | ||
* If an impossible coercion to an unrelated type is attempted, it returns `never`. | ||
* | ||
* This behavior is modeled after TypeScript's type assertion using the `as` operator. | ||
* @see {@link Type._$cast} for a more permissive version of this type that only performs downcasts or coercions to unrelated types. | ||
* | ||
* @template T - The type to assert. | ||
* @template U - The type to assert to. | ||
* | ||
* @example | ||
* type T0 = _$assert<true, true> // true | ||
* type T1 = _$assert<boolean, true> // true | ||
* type T2 = _$assert<true, boolean> // boolean | ||
* type T3 = _$assert<boolean, 0> // never | ||
*/ | ||
export type _$assert<T, U> = [T] extends [U] ? U : [U] extends [T] ? U : never | ||
|
||
interface Assert_T<T> extends Kind.Kind { | ||
f(x: this[Kind._]): _$assert<typeof x, T> | ||
} | ||
|
||
/** | ||
* `Assert` is a type-level function that casts a type `T` to a type `U`, but only if `U` is a more or less specific version of `T`. | ||
* If an impossible coercion to an unrelated type is attempted, it returns `never`. | ||
* | ||
* This behavior is modeled after TypeScript's type assertion using the `as` operator. | ||
* @see {@link Type._$cast} for a more permissive version of this function that only performs downcasts or coercions to unrelated types. | ||
* | ||
* @template T - The type to assert. | ||
* @template U - The type to assert to. | ||
* | ||
* @example | ||
* type T0 = $<$<Assert, true>, true> // true | ||
* type T1 = $<$<Assert, boolean>, true> // true | ||
* type T2 = $<$<Assert, true>, boolean> // boolean | ||
* type T3 = $<$<Assert, boolean>, 0> // never | ||
*/ | ||
export interface Assert extends Kind.Kind { | ||
f(x: this[Kind._]): Assert_T<typeof x> | ||
} |
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,3 +1,4 @@ | ||
export * from './assert' | ||
export * from './cast' | ||
export * from './display' | ||
export * from './infer' | ||
|