All types could be used with any value of strictNullChecks
option of tsconfig
.
Omits keys if they exists, otherwise does nothing
type X = TryOmit<{ a: 1; b: 2 }, 'c'>; // { a: 1; b: 2 }
type X = TryOmit<{ a: 1; b: 2; c: 3 }, 'c'>; // { a: 1; b: 2 }
Returns instance type if constructor provided, otherwise returns never
class Test {}
type X = InstanceOf<typeof Test>; // Test
type X = InstanceOf<{}>; // never
Alias for Record<string, T>
Returns true
if T
is any
and false
otherwise
type X = IsAny<null>; // false
type X = IsAny<undefined>; // false
type X = IsAny<any[]>; // false
type X = IsAny<never>; // false
type X = IsAny<unknown>; // false
type X = IsAny<number>; // false
type X = IsAny<any>; // true
Returns true
if T
describes an object and false
otherwise
type X = IsObject<number>; // false
type X = IsObject<any[]>; // false
type X = IsObject<Function>; // false
type X = IsObject<never>; // false
type X = IsObject<unknown>; // false
type X = IsObject<any>; // false
type X = IsObject<null>; // false
type X = IsObject<undefined>; // false
type X = IsObject<object>; // true
type X = IsObject<{}>; // true
type X = IsObject<Indexed>; // true
type X = IsObject<Indexed | Object | {}>; // true
type X = IsObject<Indexed & Object & {}>; // true
Returns true
if T
is tuple and false
otherwise
type X = IsTuple<[any]>; // true
type X = IsTuple<any[]>; // false
Returns true
if T1
and T2
are the same and false
otherwise
type X = Exact<{ x: 1 }, { x: 2 }>; // false
type X = Exact<{ x: 1 }, { x: 1 }>; // true
Known issues:
- type is not designed to handle function-types
- type compares optional properties only if they represented in both parameters
type X = Exact<{ x: { y?: string }, { x: { y?: number } }>; // false
type X = Exact<{ x: { y?: string }, { x: {} }>; // true - only T1 has "y" key and it's optional
Picks properties of particular type from the source type
type X = ExtractByType<
{
a: any;
b: number;
c: unknown;
d: never;
e: null;
f: undefined;
g?: number;
},
number
>; /* {
b: number;
g?: number;
} */
Returns args type if function type provided, otherwise returns never
type X = ExtractArgs<(a: number, b: string) => void>; // [number, string]
Replaces return type for provided function type
type X = ReplaceReturn<(a: number, b: string) => void, string>; // (a: number, b: string) => string
Infers return type, and in case a promise returned, infers promise resolution type
type X = ExtractAsyncReturn<(a: number, b: string) => void>; // void
type X = ExtractAsyncReturn<(a: number, b: string) => Promise<void>>; // void
If T
consists of indexed declaration and particular keys, this keys will be returned, if no such keys unknown
returned
type X = KnownKeys<{
[str: string]: any;
[num: number]: unknown;
x: number;
y: string;
}>; // 'x' | 'y'
Creates new type from T
by picking known keys
type X = PickKnown<{
[str: string]: any;
[num: number]: unknown;
x: number;
y: string;
}>; /* {
x: number;
y: string;
} */
Returns keys marked as optional
type X = GetOptionalKeys<{ a: number; b?: undefined; c?: {} }>; // 'b' | 'c'
Creates new type from T
by picking optional keys
type X = GetOptional<{ a: undefined; b?: string; c?: undefined }>; // { b?: string; c?: undefined }
Creates new type from T
by picking required keys
type X = GetRequired<{ a: null; b?: string; c?: {} }>; // { a: null }
Recursively makes all keys optional
type X = DeepPartial<{
s: {
b: {
c: number;
}[];
};
}>; /*
{
s?: {
b?: {
c?: number;
}[];
};
} */
Recursively merge two types
type X = Merge<
{
a: string;
b: { c: Array<{ d: number; d2: boolean }> };
[x: number]: { e?: any };
},
{
b: { c: Array<{ d?: string }>; c2: number };
[x: number]: { e: boolean };
f: null;
}
>; /* {
a: string;
b: { c: Array<{ d?: string; d2: boolean }>; c2: number };
[x: number]: { e: boolean };
f: null;
} */