Skip to content

Instantly share code, notes, and snippets.

@blemoine
Last active March 31, 2018 20:40
Show Gist options
  • Save blemoine/46fe19b1e544d981a605a5b5fa5dedcb to your computer and use it in GitHub Desktop.
Save blemoine/46fe19b1e544d981a605a5b5fa5dedcb to your computer and use it in GitHub Desktop.
declare class RangeValueTag<Min extends number, Max extends number> {
private __minValue: Min;
private __maxValue: Max;
}
type InRangeNumber<Min extends number, Max extends number> =
number & RangeValueTag<Min, Max>;
function isInRangeNumber<Min extends number, Max extends number>(
value: number,
min: Min,
max: Max
): value is InRangeNumber<Min, Max> {
return value > min && value < max;
}
const a = 100;
if (isInRangeNumber(a, 10, 1000)) {
const b: InRangeNumber<10, 1000> = a; //compile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment