Skip to content

Instantly share code, notes, and snippets.

View [1]ts_typejson.ts
type _Cast<T, U> = T extends U ? T : U;
type Spaces1 = ' ' | '\r' | '\n';
type Spaces2 = `${Spaces1}${Spaces1}`;
type Spaces3 = `${Spaces2}${Spaces1}`;
type Spaces4 = `${Spaces3}${Spaces1}`;
type Trim<T> =
_Cast<T, string> extends `${Spaces1} ${infer Rest}` ? Trim<Rest> :
_Cast<T, string> extends `${Spaces1} ${infer Rest}` ? Trim<Rest> :
_Cast<T, string> extends `${Spaces1} ${infer Rest}` ? Trim<Rest> :
@jet2jet
jet2jet / ts_typecalc.ts
Last active Oct 8, 2020
Addition and subtraction for numeric types in TypeScript 4.1 (https://www.pg-fl.jp/program/tips/ts_typecalc.htm (japanese))
View ts_typecalc.ts
// for license: see https://gist.github.com/jet2jet/93fab0cbd08e89bf47f81835a2dfe46c
type NumericChars = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type TupleLength<T extends any[]> =
any[] extends T ? never : T['length'];
// from microsoft/TypeScript#40336
type Split<S extends string, D extends string> =
string extends S ? string[] :
S extends '' ? [] :
S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] :
@jet2jet
jet2jet / LICENSE
Created Oct 4, 2020
The license for public gists under https://gist.github.com/jet2jet
View LICENSE
Copyright (C) jet2jet (jet / ジェット)
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
You can’t perform that action at this time.