Skip to content

Latest commit

 

History

History
1170 lines (714 loc) · 19.9 KB

README.md

File metadata and controls

1170 lines (714 loc) · 19.9 KB

@lambda-fn/basis

@lambda-fn/basis

Table of contents

Type aliases

Functions

Type aliases

AbstractConstructor

Ƭ AbstractConstructor<I, Args>: (...args: Args) => I

Type parameters

Name Type
I I
Args extends unknown[]

Type declaration

• (...args)

Helper for create abstract constructor signatures

Parameters
Name Type
...args Args

AnyAbstractConstructor

Ƭ AnyAbstractConstructor<I>: (...args: any[]) => I

Type parameters

Name Type
I any

Type declaration

• (...args)

Helper for create abstract constructor type predicates

Parameters
Name Type
...args any[]

AnyConstructor

Ƭ AnyConstructor<I>: (...args: any[]) => I

Type parameters

Name Type
I any

Type declaration

• (...args)

Helper for create constructor type predicates

Parameters
Name Type
...args any[]

AnyFn

Ƭ AnyFn<R>: (...args: any[]) => R

Type parameters

Name Type
R any

Type declaration

▸ (...args): R

Helper for create function type predicates

Parameters
Name Type
...args any[]
Returns

R


AsBoolean

Ƭ AsBoolean<T>: T extends Falsy ? false : true

Transform any input type to true or false representation

Type parameters

Name
T

AsFactory

Ƭ AsFactory<I, C>: UnboundFn<I, ConstructorParameters<C>>

Transforms constructor signature to function signature

Type parameters

Name Type
I I
C extends AnyConstructor<I>

AsFunction

Ƭ AsFunction<T>: () => T

Type parameters

Name
T

Type declaration

▸ (): T

Function representation of any type

Returns

T


AsString

Ƭ AsString<T, Default>: T extends Exclude<Primitive, symbol> ? `${T}` : T extends {} ? Strong<S, string, Default> : Default

String representation of any type

Type parameters

Name Type
T T
Default extends string = string

Conditional

Ƭ Conditional<Condition, TrueType, FalseType>: Condition extends true ? TrueType : FalseType

Checks condition is strongly true type

returns 2nd parameter if 1st parameter is true or 3rd parameter otherwise

Type parameters

Name Type
Condition extends boolean
TrueType TrueType
FalseType never

Constructor

Ƭ Constructor<I, Args>: (...args: Args) => I

Type parameters

Name Type
I I
Args extends unknown[]

Type declaration

• (...args)

Helper for create constructor signatures

Parameters
Name Type
...args Args

Equal

Ƭ Equal<A, B>: A extends B ? B extends A ? true : false : false

Checks two types are representation of the some type

Type parameters

Name
A
B

Falsy

Ƭ Falsy: false | 0 | 0n | null | undefined | ""

May contains all possible falsy values
Notice: NaN is falsy value but hasn't literal type and not included in this type


Fn

Ƭ Fn<R, Args, This>: unknown extends This ? (...args: Args) => R : (this: This, ...args: Args) => R

Helper for create function signatures

Type parameters

Name Type
R R
Args extends unknown[]
This unknown

Keys

Ƭ Keys<T>: AsString<Exclude<keyof T, symbol>>[]

Strong variant of keyof operator

Type parameters

Name
T

LiteralBase

Ƭ LiteralBase<Literal>: { [K in keyof PrimitiveMap]: Literal extends PrimitiveMap[K] ? PrimitiveMap[K] : never }[keyof PrimitiveMap]

Get base type from literal type

Type parameters

Name Type
Literal extends Primitive

LiteralUnion

Ƭ LiteralUnion<Literals, Basis>: Literals | Basis & {}

Create primitive type with IDE autocompletion for most used literals

Type parameters

Name Type
Literals Literals
Basis extends Primitive = LiteralBase<Literals & Primitive>

NRecord

Ƭ NRecord<T>: Record<number, T>

Shorthand for Record with number keys

Type parameters

Name Type
T unknown

Opaque

Ƭ Opaque<T, Tag>: T & {}

Type parameters

Name
T
Tag

Primitive

Ƭ Primitive: PrimitiveMap[keyof PrimitiveMap]

Shorthand for all primitive types union


Recalculate

Ƭ Recalculate<T>: T extends unknown ? { [K in keyof T]: T[K] } : never

Recalculates type to simple representation

Type parameters

Name
T

SRecord

Ƭ SRecord<T>: Record<string, T>

Shorthand for Record with string keys

Type parameters

Name Type
T unknown

SelectByKeyValue

Ƭ SelectByKeyValue<T, Key, Value>: Recalculate<T & { [_ in Key]: Value }>

Selects variants of union by key-value pair

Type parameters

Name Type
T T
Key extends keyof T
Value extends T[Key]

SelectivePartial

Ƭ SelectivePartial<T, K>: Recalculate<Required<Pick<T, Exclude<keyof T, K>>> & Partial<Pick<T, K>>>

Makes some keys of type optional

Type parameters

Name Type
T T
K extends keyof T

SelectiveRequired

Ƭ SelectiveRequired<T, K>: Recalculate<Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>>

Makes some keys of type required

Type parameters

Name Type
T T
K extends keyof T

StrongBigint

Ƭ StrongBigint<Input>: Strong<Input, bigint, 0n>

Checks input type is bigint

returns input type if it is bigint or 0n otherwise

Type parameters

Name
Input

StrongBoolean

Ƭ StrongBoolean<Input>: Strong<Input, boolean, false>

Checks input type is boolean

returns input type if it is boolean or false otherwise

Type parameters

Name
Input

StrongNumber

Ƭ StrongNumber<Input>: Strong<Input, number, 0>

Checks input type is number

returns input type if it is number or 0 otherwise

Type parameters

Name
Input

StrongPropertyDescriptor

Ƭ StrongPropertyDescriptor<T, K>: {} | {} | {}

Strong variant of PropertyDescriptor

Type parameters

Name Type
T T
K extends keyof T

StrongPropertyDescriptorMap

Ƭ StrongPropertyDescriptorMap<T>: { [K in keyof T]?: StrongPropertyDescriptor<T, K> }

Strong variant of PropertyDescriptorMap

Type parameters

Name
T

StrongString

Ƭ StrongString<Input>: Strong<Input, string, AsString<Input, "">>

Checks input type is string

returns input type if it is string or '' otherwise

Type parameters

Name
Input

URecord

Ƭ URecord<T>: Record<PropertyKey, T>

Shorthand for Record with any possible keys

Type parameters

Name Type
T unknown

UnboundFn

Ƭ UnboundFn<R, Args>: Fn<R, Args, void>

Helper for create unbound (this: void) function signatures

Type parameters

Name Type
R R
Args extends unknown[]

UnboundVoidFn

Ƭ UnboundVoidFn<Args>: Fn<void, Args, void>

Helper for create unbound (this: void) function signatures that returns void

Type parameters

Name Type
Args extends unknown[]

UnionToIntersection

Ƭ UnionToIntersection<U>: U extends unknown ? Fn<unknown, [U]> : never extends Fn<unknown, [infer I]> ? I : never

Transforms union of types to intersection of that types

Type parameters

Name
U

VoidFn

Ƭ VoidFn<Args, This>: Fn<void, Args, This>

Helper for create function signatures that returns void

Type parameters

Name Type
Args extends unknown[]
This unknown

Functions

False

False(): false

Functional representation of false

Returns

false

always false


Opaque

Opaque<T, Tag>(): OpaqueFactory<T, Tag>

Create wrapper for attach opaque type to value

Type parameters

Name
T
Tag

Returns

OpaqueFactory<T, Tag>


True

True(): true

Functional representation of true

Returns

true

always true


asBoolean

asBoolean<T>(value): AsBoolean<T>

Transform any value to boolean representation with strong types
Notice: for NaN it returns false with type true

see Falsy

Type parameters

Name
T

Parameters

Name Type Description
value T Any value to transform

Returns

AsBoolean<T>

true of false


asFactory

asFactory<I, C>(constructor): AsFactory<I, C>

Transforms constructor to factory

example

class A {}
const createA = asFactory(A);
// Valid and returns instance of A
const a = createA();

Type parameters

Name Type
I I
C extends AnyConstructor<I>

Parameters

Name Type Description
constructor C Any non abstract constructor

Returns

AsFactory<I, C>

Function with some params but callable without new operator


asFunction

asFunction<T>(value): AsFunction<T>

Transforms any value to function that always returns this value

Type parameters

Name
T

Parameters

Name Type
value T

Returns

AsFunction<T>


asString

asString<T>(value): AsString<T, string>

Convert any value to string representation with strong types

Type parameters

Name
T

Parameters

Name Type
value T

Returns

AsString<T, string>


assert

assert(condition, message, errorConstructor?): asserts condition

Asserts condition is true

throws Error if condition is false

Parameters

Name Type Description
condition boolean Condition
message string -
errorConstructor? ErrorConstructor -

Returns

asserts condition

assert(condition, error): asserts condition

Asserts condition is true

throws Error if condition is false

Parameters

Name Type Description
condition boolean Condition
error Error -

Returns

asserts condition


assign

assign<Target, Sources>(target, ...sources): UnionToIntersection<Target | Sources[number]>

Object.assign with strong types

Type parameters

Name Type
Target Target
Sources extends URecord<unknown>[]

Parameters

Name Type
target Target
...sources Sources

Returns

UnionToIntersection<Target | Sources[number]>


bind

bind<R, Args, This>(f, thisArg): (...args: Args) => R

Wrap function to unbounded variant

Type parameters

Name Type
R R
Args extends unknown[]
This This

Parameters

Name Type Description
f Fn<R, Args, This> Any function
thisArg This Context (this) for function f

Returns

fn

Unbounded function

▸ (...args): R

Parameters
Name Type
...args Args
Returns

R


contextify

contextify<R, Args, This>(f): Fn<R, Args, This>

Wrap function to bounded variant

example

const originalMethod = SomeClass.prototype.method;
SomeClass.prototype.method = contextify((self: SomeClass, arg: string) => {
    // ... some patch ...
    return originalMethod.call(self, arg);
});

Type parameters

Name Type
R R
Args extends unknown[]
This This

Parameters

Name Type Description
f (...args: [This, ...Args[]]) => R Any function

Returns

Fn<R, Args, This>

Function that calls original function with this as 1st argument


create

create<I, C>(constructor, ...args): I

Helper for create instance from constructor

Type parameters

Name Type
I I
C extends AnyConstructor<I>

Parameters

Name Type Description
constructor C Any non abstract constructor
...args ConstructorParameters<C> constructor arguments

Returns

I

created instance


defineProperties

defineProperties<T>(target, properties): T

Object.defineProperties with strong types

Type parameters

Name
T

Parameters

Name Type
target T
properties StrongPropertyDescriptorMap<T>

Returns

T


defineProperty

defineProperty<T, K>(target, property, descriptor): T

Object.defineProperty with strong types

Type parameters

Name Type
T T
K extends string | number | symbol

Parameters

Name Type
target T
property K
descriptor StrongPropertyDescriptor<T, K>

Returns

T


identity

identity<T>(value): T

Returns argument

Type parameters

Name
T

Parameters

Name Type
value T

Returns

T


keys

keys<T>(target, includeNonEnumerable?): Keys<T>

Object.keys / Object.getOwnPropertyNames with strong types

Type parameters

Name
T

Parameters

Name Type Default value Description
target T undefined Any object
includeNonEnumerable boolean false Set it to true for include non enumerable keys

Returns

Keys<T>

Keys array


noop

noop(): void

It's doing nothing

Returns

void


panic

panic(message, errorConstructor?): never

Shorthand for throw error

Parameters

Name Type
message string
errorConstructor? ErrorConstructor

Returns

never

panic(error): never

Shorthand for throw error

Parameters

Name Type
error Error

Returns

never


partial

partial<R, A1, A2, This>(f, ...args1): Fn<R, A2, This>

Partial application

Type parameters

Name Type
R R
A1 extends unknown[]
A2 extends unknown[]
This unknown

Parameters

Name Type Description
f Fn<R, [...A1[], ...A2[]], This> Any function
...args1 A1 1st part of function arguments

Returns

Fn<R, A2, This>

Function with 2nd part of function arguments


set

set<T, K>(target, key, value): T

Helper for set property to object

Type parameters

Name Type
T extends EmptyObject
K extends string | number | symbol

Parameters

Name Type Description
target T Any object
key K Object property key
value T[K] Property value

Returns

T

target