Aller au contenu principal

@w5s/core-type

W5S Core type modules (@w5s/core-type)

NPM Version License

Installation

npm install @w5s/core-type

Usage

Example

import type { Nullable } from '@w5s/core-type';

export function someFunction(_input: Nullable<number>) {}

License

MIT © Julien Polo julien.polo@gmail.com

Index

Type Aliases

AnyFunction

AnyFunction: (...args: any[]) => any

A function that takes any kind of parameters and returns anything

@example
function decorate<F extends AnyFunction>(fn: F): F {
// ...
}

Awaitable

Awaitable<T>: T | PromiseLike<T>

Type for something that can be used with await. It can be either T or Promise<T>

@see

Type parameters

  • T

Day

Day: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31

Day of the month

EmptyObject

EmptyObject: Record<PropertyKey, never>

ExpectAssertion

ExpectAssertion: ExpectAssertionObject & { not: ExpectAssertionObject }

Hour

Hour: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23

An hour of the day

JSONArray

JSONArray: JSONValue[]

Array of JSONValue

JSONObject

JSONObject: {}

Record of JSONValue

JSONPrimitive

JSONPrimitive: null | boolean | number | string

Any JSON primitive

  • null
  • boolean
  • number
  • string

JSONValue

Any valid JSON value

Minute

Minute: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59

Minute of an hour

Month

Month: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12

Month of the year

Nullable

Nullable<T>: null | undefined | T

A type that can be either undefined, null, or T

@example
type NullableNumber = Nullable<number>;

function someFunction(value: NullableNumber) {
// value is number | undefined | null
}

Type parameters

  • T = never

PartialKeys

PartialKeys<T, Keys>: Pretty<Omit<T, Keys> & Partial<Pick<T, Keys>>>

Return a partial type of T for keys in Keys

@example
type T = { required: boolean; optional1: string; optional2: string; };
type OptionalT = PartialKeys<T, 'optional'>; // { required: boolean; optional1?: string; optional2?: string; };

Type parameters

  • T
  • Keys: keyof T

Pretty

Pretty<T>: { [ KeyType in keyof T ]: T[KeyType] } & {}

Flatten the type. Useful for IDE information.

@example
type A = { a: boolean };
type B = { b: string };
type AB = Pretty<A & B>; // { a: boolean; b: string; }

Type parameters

  • T

Primitive

Primitive: string | number | symbol | bigint | boolean | null | undefined

Primitive values are immutable data at the lowest level of the language.

There are 7 primitive types

  • string
  • number
  • symbol
  • bigint
  • boolean
  • null
  • undefined
@see

RequiredKeys

RequiredKeys<T, Keys>: Pretty<Omit<T, Keys> & Required<Pick<T, Keys>>>

Make specified Keys properties in T required

@example
type T = { foo?: boolean, bar?: string }
type RequiredT = RequiredKeys<T, 'bar'>; // { foo?: boolean; bar: string };

Type parameters

  • T
  • Keys: keyof T

Second

Second: Minute

Second of a minute

Tag

Tag<T, Value>: { @@tag: { readonly [ K in T ]: Value } }

Enhance Base by adding tags. Every tag is prefixed by @@ as a convention to never be used by runtime code

@example
type PositiveNumber = number & Tag<'Positive'>;
const isPositive = (n: number): n is PositiveNumber => n >= 0;
const squareRoot = (n: PositiveNumber): PositiveNumber => Math.sqrt(n) as PositiveNumber;
const value = 0;
squareRoot(value); // tsc: Error
if (isPositive(value)) {
squareRoot(value); // tsc: Passed
}

Type parameters

  • T: string | symbol
  • Value = true

ValueOf

ValueOf<T, Keys>: T[Keys]

Extract all values from the keys Keys of T. If Keys is omitted, all keys are used.

@example
type AllValues = ValueOf<{ Foo: boolean; Bar: 'bar'; Baz: 'baz' }> // 'bar' | 'baz' | boolean
type SomeValues = ValueOf<{ Foo: boolean; Bar: 'bar'; Baz: 'baz' }, 'Baz' | 'Bar'> // 'bar' | 'baz'

Type parameters

  • T
  • Keys: keyof T = keyof T

Year

Year: number

Year number