npm

@pastable/typings
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

@pastable/typings

A bunch of utility types.

List

PrimitiveValue

Quickly target a primitive value.

type PrimitiveValue = string | number | boolean;

Primitive

Either PrimitiveValue or an array of PrimitiveValue

type Primitive = PrimitiveValue | Array<PrimitiveValue>;

ObjectLiteral

Pre-configured record for most common uses.

type ObjectLiteral<T = any> = Record<string, T>;

JSONValue

Possible JSON value. https://github.com/Microsoft/TypeScript/issues/3496

type JSONValue = PrimitiveValue | JSONObject | JSONArray;

JSONObject

JSON object.

interface JSONObject {
    [x: string]: JSONValue;
}

JSONArray

JSON array.

interface JSONArray {
    [x: string]: JSONValue;
}

Unpacked

Extract a generic type from where its used. https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html

type Unpacked<T> = T extends (infer U)[]
    ? U
    : T extends (...args: any[]) => infer U
    ? U
    : T extends Promise<infer U>
    ? U
    : T;

CType

Get the constructor of a class.

type CType<T = any> = new (...args: any[]) => T;

AnyFunction

Literraly allow for any function, has return/args generics.

type AnyFunction<Return = any, Args = any> = (...args: Args[]) => Return;

FunctionArguments

Get arguments as array from a function type.

type FunctionArguments<T extends Function> = T extends (...args: infer R) => any ? R : never;

// ex
type Ding = (aaaa: string, bbb: number) => void;
type Dong = FunctionArguments<Ding>;

ArrayUnion

Get a union type from a const array.

type ArrayUnion<T extends Readonly<string[]>> = T[number];

// ex
const keys = ["aaa", "bbb", "ccc"] as const;
type Keys = ArrayUnion<typeof keys>;
// type Keys = "aaa" | "bbb" | "ccc"

DeepPartial

Same as Partial<T> but goes deeper and makes Partial<T> all its properties and sub-properties. https://github.com/typeorm/typeorm/blob/master/src/common/DeepPartial.ts

type DP<T> = {
    [P in keyof T]?: T[P] extends Array<infer U>
        ? Array<DeepPartial<U>>
        : T[P] extends ReadonlyArray<infer U>
        ? ReadonlyArray<DeepPartial<U>>
        : DeepPartial<T[P]>;
type DeepPartial<T> = { [P in keyof T]?: T[P] | DP<T> }

NonUndefined

Ensure that <T> is not undefined.

type NonUndefined<A> = A extends undefined ? never : A;

NonFunctionKeys

Can be used to retrieve class properties.

type NonFunctionKeys<T extends object> = {
    [K in keyof T]-?: NonUndefined<T[K]> extends Function ? never : K;
}[keyof T];

// ex
class Example {
    stringType: string;
    numberType: number;
    booleanType: boolean;
    nullType: null;
    functionType: () => void;
}
type Dang = NonFunctionKeys<Example>;
// type Dang = "stringType" | "numberType" | "booleanType"

Package Sidebar

Install

npm i @pastable/typings

Weekly Downloads

11

Version

0.0.8

License

none

Unpacked Size

7.75 kB

Total Files

14

Last publish

Collaborators

  • astahmer