Skip to content

Instantly share code, notes, and snippets.

@JSuder-xx
JSuder-xx / micro_type_predicate.ts
Last active February 7, 2021 16:09
Micro Api for building type predicates in TypeScript.
/** Micro Api for building type predicates */
module TypePredicateApi {
export type TypePredicate<refined> = (val: any) => val is refined;
export type TypeOfTypePredicate<refiner> = refiner extends TypePredicate<infer refined> ? refined : never;
export type TypePredicateMap = { [propertyName: string]: TypePredicate<any> }
export type ObjectFromTypePredicateMap<map extends { [propertyName: string]: TypePredicate<any> }> = {
[propertyName in keyof map]: TypeOfTypePredicate<map[propertyName]>;
}
/** Create a predicate which tests for an object. */
@JSuder-xx
JSuder-xx / constrained_fluent_builder.ts
Last active May 4, 2022 02:54
Fluent Builder Type API which transforms any arbitrary fluent builder type into a fluent builder type enforcing call count dependencies between methods (where dependencies are represented as a type). Employs Mapped, Conditional, and Literal Types to demonstrate both TypeScript's proximity to dependent typing and the practical value.
/**
* Imagine a Fluent Builder where some subset of the fluent methods are valid to call
* * after one or more fluent methods have been called
* * before one or more fluent methods have been called
* * limited number of times.
*
* There is no way to enforce such constraints in statically typed languages such as C++, C# or Java when using a single builder
* class/interface. Developers would need to author many interfaces to represent the different shapes and would likely need to
* author many versions of the builder itself (proxies with a specific signature delegating to an underlying source builder).
*
@JSuder-xx
JSuder-xx / expressions_refining_environment.ts
Last active January 7, 2020 02:43
An application of TypeScript conditional/mapped/literal types to Expression construction which yields an inferred static type for the Environment that would satisfy the needs of the Expression. This is magic.
/**
* When implementing the evaluation of a tree of expressions, an environment/context/memory representation is typically passed as an
* argument (or injected into the constructor of the interpreter class for OO realizations of the pattern) in order to give the
* expressions access to a memory store.
*
* The *Type* of the memory does not normally relate to any specific constructed expression ex. the memory may be implemented as an
* _arbitrary_ dictionary of key/value pairs. As such, there is no way to know if a type or instance of a dictionary of values can
* satisfy the needs to a specific constructed expression.
*
* For example, the following expression reads a number named 'x' from the environment and therefore the expression _requires_ an x of type number
@JSuder-xx
JSuder-xx / fluent_mapper_builder_gist.ts
Created January 12, 2019 17:59
Explicitly declare modifications to an existing object type via a fluent interface which yields a mapping function to the derived type. Demonstration of TypeScript meta-programming through mapped and conditional types.
/**
* _Explicitly declare modifications_ to an existing object type via a fluent interface which yields a mapping function
* from the original data type to the type derived from the modification commands.
*
* The value over authoring a simple mapping function directly
* * harder to make a mistake due to the explicit nature
* * easier to read.
*
* This is _not_ production quality code but rather a _proof of concept_ gist for applying conditional/mapped
* types to mapper generation. A real-world usage might involve also generating the type-guard function
@chambart
chambart / gist:a3b05c0895f6afb9cf01
Created July 3, 2015 15:22
Peano numbers with ocaml functors
type zero = unit
type 'a succ = unit -> 'a
type 'a nat =
| Zero : zero nat
| Succ : 'a nat -> 'a succ nat
module type T = sig
type t
val v : t nat
@jstangroome
jstangroome / Get-VSSolutionReferences.ps1
Created August 30, 2010 09:34
Get-VSSolutionReferences.ps1
#requires -version 2.0
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
[string]
$Path
)
$ErrorActionPreference = 'Stop'