types
package @remirror/types
interface Coords
Defines coordinates returned by the [[EditorView.coordsAtPos
]] function.
Signature:
export interface Coords
property bottom
Vertical distance from the top of the page viewport to the bottom side of the described position (px).
Signature:
bottom: number;
property left
Horizontal distance from the left of the page viewport to the left side of the described position (px).
Signature:
left: number;
property right
Horizontal distance from the left of the page viewport to the right side of the described position (px).
Signature:
right: number;
property top
Vertical distance from the top of the page viewport to the top side of the described position (px).
Signature:
top: number;
interface Flavoring
Used by Flavor
to mark a type in a readable way.
Signature:
export interface Flavoring<Name>
property [_flavor]
Signature:
readonly [_flavor]?: Name;
interface ObjectMark
A JSON representation of a prosemirror Mark.
Signature:
export interface ObjectMark
property attrs
Signature:
attrs?: Record<string, Literal>;
property type
Signature:
type: string;
interface Shape
An object with string keys and values of type any
Signature:
export interface Shape
Signature:
[key: string]: any;
type And
A shorthand for creating and intersection of two object types.
Signature:
export type And<Type extends Shape, Other extends Shape> = Type & Other;
References: Shape
type AnyConstructor
Matches any constructor type.
Signature:
export type AnyConstructor<Type = any> = new (...args: any[]) => Type;
type AnyFunction
Concisely and cleanly define an arbitrary function.
Signature:
export type AnyFunction<Type = any> = (...args: any[]) => Type;
Remarks:
Taken from simplytyped
Useful when designing many api's that don't care what function they take in, they just need to know what it returns.
type Array1
An array which must include the first item.
Signature:
export type Array1<Type> = MinArray<Type, 1>;
References: MinArray
type Array2
An array which must include the first 2 items.
Signature:
export type Array2<Type> = MinArray<Type, 2>;
References: MinArray
type Array3
An array which must include the first 3 items.
Signature:
export type Array3<Type> = MinArray<Type, 3>;
References: MinArray
type Brand
Create a "branded" version of a type. TypeScript won't allow implicit conversion to this type
Signature:
export type Brand<Type, B> = Type & Branding<B>;
type ConditionalReturnKeys
Conditionally pick keys which are functions and have the requested return type.
Signature:
export type ConditionalReturnKeys<Base, Return> = NonNullable<{
[Key in keyof Base]: Base[Key] extends AnyFunction<infer R> ? R extends Return ? Key : never : never;
}[keyof Base]>;
References: AnyFunction
type ConditionalReturnPick
Pick the properties from an object that are methods with the requested Return
type.
Signature:
export type ConditionalReturnPick<Base, Return> = Pick<Base, ConditionalReturnKeys<Base, Return>>;
References: ConditionalReturnKeys
type DeepPartial
A recursive partial type. Useful for object that will be merged with defaults.
Signature:
export type DeepPartial<Type> = Type extends object ? {
[K in keyof Type]?: DeepPartial<Type[K]>;
} : Type;
References: DeepPartial
type DeepString
Converts every nested type to a string.
Signature:
export type DeepString<Type> = Type extends object ? {
[K in keyof Type]: DeepString<Type[K]>;
} : string;
References: DeepString
type Diff
Get the diff between two types. All identical keys are stripped away.
Signature:
export type Diff<A, B> = Omit<A, keyof B> & Omit<B, keyof A>;
Remarks:
type Fun = Diff<{notFun: false, fun: true}, {notFun: true, wow: string}>;
// => { fun: true, wow: string }
type EmptyShape
An alternative to usage of {}
as a type.
Signature:
export type EmptyShape = Record<never, never>;
type Flavor
Create a "flavored" version of a type. TypeScript will disallow mixing flavors, but will allow unflavored values of that type to be passed in where a flavored version is expected. This is a less restrictive form of branding.
Signature:
export type Flavor<Type, Name> = Type & Flavoring<Name>;
References: Flavoring
type FlipPartialAndRequired
Reverses the partial and required keys for the type provided. If it was a required property it becomes a partial property and if it was a partial property it becomes a required property.
Signature:
export type FlipPartialAndRequired<Type extends Shape> = PickPartial<Type> & Partial<PickRequired<Type>>;
References: Shape, PickPartial, PickRequired
type GetRequiredKeys
Get all the keys for required properties on this type.
Signature:
export type GetRequiredKeys<Type extends Shape> = keyof ConditionalPick<KeepPartialProperties<Type>, NeverBrand>;
References: Shape, KeepPartialProperties
type IfEmpty
Conditional type which checks if the provided Type
is and empty object (no properties). If it is uses the Then
type if not falls back to the Else
type.
Signature:
export type IfEmpty<Type extends object, Then, Else> = keyof Type extends never ? Then : Else;
type IfHasRequiredProperties
Checks the type provided and if it has any properties which are required it will return the Then
type. When none of the properties are required it will return the Else
type.
Signature:
export type IfHasRequiredProperties<Type extends object, Then, Else> = IfNoRequiredProperties<Type, Else, Then>;
References: IfNoRequiredProperties
Remarks:
This is a reverse of the IfNoRequiredProperties
type.
type IfMatches
Condition that checks if the keys of the two objects match. If so, respond with Then
otherwise Else
.
Signature:
export type IfMatches<A, B, Then, Else> = IfEmpty<Diff<A, B>, Then, Else>;
type IfNoRequiredProperties
A conditional check on the type. When there are no required keys it outputs the Then
type, otherwise it outputs the Else
type.
Signature:
export type IfNoRequiredProperties<Type extends object, Then, Else> = GetRequiredKeys<Type> extends NeverBrand ? Then : Else;
References: GetRequiredKeys
Remarks:
This is useful for dynamically setting the parameter list of a method call depending on whether keys are required.
type IndexUnionFromTuple
Extract the valid index union from a provided tuple.
import { IndexUnionFromTuple } from '@remirror/core-types';
const tuple = ['a', 'b', 'c'];
type Index = IndexUnionFromTuple<typeof tuple> => 0 | 1 | 2
Signature:
export type IndexUnionFromTuple<Tuple extends readonly unknown[]> = Tuple extends Tuple ? number extends Tuple['length'] ? number : _IndexUnionFromTuple<[], Tuple['length']> : never;
type JsonPrimitive
Matches any valid JSON primitive value.
Signature:
export type JsonPrimitive = string | number | boolean | null;
type KeepPartialProperties
Keeps the partial properties of a type unchanged. Transforms the rest to never
.
Signature:
export type KeepPartialProperties<Type extends Shape> = {
[Key in keyof Type]: Type[Key] extends undefined ? Type[Key] : NeverBrand;
};
References: Shape
type Listable
Allow a type of a list of types.
Signature:
export type Listable<Type> = Type | Type[];
type Literal
All the literal types
Signature:
export type Literal = string | number | boolean | undefined | null | void | object;
type MakeNullable
Makes specified keys of an interface nullable while the rest stay the same.
Signature:
export type MakeNullable<Type extends object, Keys extends keyof Type> = Omit<Type, Keys> & {
[Key in Keys]: Type[Key] | null;
};
type MakeOptional
Makes specified keys of an interface optional while the rest stay the same.
Signature:
export type MakeOptional<Type extends object, Keys extends keyof Type> = Omit<Type, Keys> & {
[Key in Keys]+?: Type[Key];
};
type MakeReadonly
Makes specified keys of an interface readonly.
Signature:
export type MakeReadonly<Type extends object, Keys extends keyof Type> = Omit<Type, Keys> & {
+readonly [Key in Keys]: NonNullable<Type[Key]>;
};
type MakeRequired
Makes specified keys of an interface Required while the rest remain unchanged.
Signature:
export type MakeRequired<Type extends object, Keys extends keyof Type> = Omit<Type, Keys> & {
[Key in Keys]-?: Type[Key];
};
type MakeUndefined
Makes specified keys of an interface optional while the rest stay the same.
Signature:
export type MakeUndefined<Type extends object, Keys extends keyof Type> = Omit<Type, Keys> & {
[Key in Keys]: Type[Key] | undefined;
};
type MinArray
Create a type for an array (as a tuple) which has at least the provided Length
.
This can be useful when noUncheckedIndexedAccess
is set to true in the compiler options. Annotate types when you are sure the provided index will always be available.
import { MinArray } from '@remirror/core-types';
MinArray<string, 2>; // => [string, string, ...string[]];
Signature:
export type MinArray<Type, Length extends number> = Length extends Length ? number extends Length ? Type[] : _MinArray<Type, Length, []> : never;
type NonNullableShape
Signature:
export type NonNullableShape<Type extends object> = {
[Key in keyof Type]: NonNullable<Type[Key]>;
};
type Nullable
Makes a type nullable or undefined.
Signature:
export type Nullable<Type> = Type | null | undefined;
type PartialWithRequiredKeys
Make the whole interface partial except for some specified keys which will be made required.
Signature:
export type PartialWithRequiredKeys<Type extends object, Keys extends keyof Type> = Partial<Pick<Type, Exclude<keyof Type, Keys>>> & Required<Pick<Type, Keys>>;
type PickPartial
Pick the partial
properties from the provided Type and make them all required.
Signature:
export type PickPartial<Type extends Shape> = {
[Key in keyof ConditionalExcept<KeepPartialProperties<Type>, NeverBrand>]-?: Type[Key];
};
References: Shape, KeepPartialProperties
type PickRequired
Only pick the required
(non-partial
) types from the given Type
.
Signature:
export type PickRequired<Type extends Shape> = {
[Key in keyof ConditionalPick<KeepPartialProperties<Type>, NeverBrand>]: Type[Key];
};
References: Shape, KeepPartialProperties
type Predicate
Creates a predicate type.
Signature:
export type Predicate<Type> = (value: unknown) => value is Type;
type ProsemirrorAttributes
Used for attributes which can be added to prosemirror nodes and marks.
Signature:
export type ProsemirrorAttributes<Extra extends object = object> = Record<string, unknown> & Remirror.Attributes & Extra & {
class?: string;
};
type RemoveFlavoring
Remove the flavoring from a type.
Signature:
export type RemoveFlavoring<Type, Name> = Type extends Flavor<infer T, Name> ? T : Type;
References: Flavor
type Replace
Replace and extend any object keys.
Signature:
export type Replace<Type, Replacements extends Shape> = Omit<Type, keyof Replacements> & Replacements;
References: Shape
type Simplify
When a type is really deep and has retained an unnecessary amount of type information, this flattens it to a single array/object/value.
TODO not using it right now as it's breaking with globally available types via namespace.
Signature:
export type Simplify<T> = T extends object | any[] ? {
[K in keyof T]: T[K];
} : T;
type StrictReplace
Replace only the current keys with different types.
Signature:
export type StrictReplace<Type, Replacements extends Record<keyof Type, unknown>> = Omit<Type, keyof Replacements> & Replacements;
type StringKey
An alternative to keyof that only extracts the string keys.
Signature:
export type StringKey<Type> = Extract<keyof Type, string>;
type TupleOf
Create a tuple of Size
from the provided Type
.
Signature:
export type TupleOf<Type, Size extends number> = Size extends Size ? number extends Size ? Type[] : _TupleOf<Type, Size, []> : never;
type TupleUnion
Returns tuple types that include every string in union TupleUnion<keyof {bar: string; leet: number }>; ["bar", "leet"] | ["leet", "bar"];
Taken from ❤️ https://github.com/microsoft/TypeScript/issues/13298\#issuecomment-692864087
Problem it is recursive and has quickly eats into the maximum depth.
Signature:
export type TupleUnion<T> = ((T extends any ? (t: T) => T : never) extends infer U ? (U extends any ? (u: U) => any : never) extends (v: infer V) => any ? V : never : never) extends (_: any) => infer W ? [...TupleUnion<Exclude<T, W>>, W] : [];
References: TupleUnion
type TupleValue
Extract the values of a tuple as a union type.
Signature:
export type TupleValue<Tuple extends readonly unknown[]> = Tuple[number];
Remarks:
const myTuple = ['a', 'b', 'c'] as const;
type MyTuple = TupleValue<typeof myTuple>; // 'a' | 'b' | 'c'
type UndefinedFlipPartialAndRequired
Reverses the partial and required keys for the type provided. If it was a required property it becomes a partial property and if it was a partial property it becomes a required property.
Signature:
export type UndefinedFlipPartialAndRequired<Type extends Shape> = UndefinedPickPartial<Type> & Partial<PickRequired<Type>>;
References: Shape, UndefinedPickPartial, PickRequired
type UndefinedPickPartial
Like pick partial but all types can still specify undefined.
Signature:
export type UndefinedPickPartial<Type extends Shape> = {
[Key in keyof PickPartial<Type>]: PickPartial<Type>[Key] | undefined;
};
References: Shape, PickPartial
type UnknownShape
An object with string keys and values of type unknown
Signature:
export type UnknownShape<Type = unknown> = Record<string, Type>;
type UseDefault
When the type is never use a default type instead.
TODO why doesn't this work
Signature:
export type UseDefault<Type, Default> = Type extends never ? Default : Type;
type Value
Extract the values of an object as a union type.
Signature:
export type Value<Type> = Type[keyof Type];
Remarks:
const myRecord = { A: 'a', B: 'b', C: 'c' } as const;
type MyRecord = Value<typeof myRecord>; // 'a' | 'b' | 'c'
type Writeable
Remove all readonly modifiers from the provided type.
Signature:
export type Writeable<Type> = {
-readonly [Key in keyof Type]: Type[Key];
};