Skip to content

Instantly share code, notes, and snippets.

@iFwu
Created August 20, 2020 03:48
Show Gist options
  • Save iFwu/11ec737e01994920430179f85f409143 to your computer and use it in GitHub Desktop.
Save iFwu/11ec737e01994920430179f85f409143 to your computer and use it in GitHub Desktop.
import {Number} from "ts-toolbelt"
type Even_<a extends string> = {
0: true,
1: false,
2: Even_<Number.Minus<a, "2">>
}[a extends "0" ? 0 : a extends "1" ? 1 : 2]
type Even<a extends number> = a extends unknown ? Even_<Number.NumberOf<a>> : never
type AssertEven<a extends any[]> = Even<a["length"]> extends true ? a : unknown
type A = AssertEven<[1,2,3]> // unknown
type B = AssertEven<[1,2,3,4]> // [1,2,3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment