Skip to content

Instantly share code, notes, and snippets.

View ts_narrowobject.ts
// 「typeof 演算子」の型を取得するためのダミー関数
function _dummy() { return typeof ''; }
type TypeofType = ReturnType<typeof _dummy>;
// 「TypeofType」は TS 4.9 時点では「'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function'」になる
// typeof 演算子の結果の文字列に対応する型を書いたマップオブジェクト
interface TypeofTypeMap {
number: number;
string: string;
boolean: boolean;
@jet2jet
jet2jet / ts_typecalc2.ts
Last active August 6, 2022 12:36
Addition and subtraction for numeric types in TypeScript 4.8 (https://www.pg-fl.jp/program/tips/ts_typecalc2.htm (japanese))
View ts_typecalc2.ts
// for license: see https://gist.github.com/jet2jet/93fab0cbd08e89bf47f81835a2dfe46c
// *** Must be used with TS 4.8 or later ***
type NumericChars = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
interface DigitToTuple {
'0': [];
'1': [''];
'2': ['', ''];
'3': ['', '', ''];
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 March 1, 2022 01:25
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 October 4, 2020 05:03
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