UNPKG

2.25 kBTypeScriptView Raw
1/**
2Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
3
4@category Class
5*/
6export type Class<T, Arguments extends unknown[] = any[]> = {
7 prototype: Pick<T, keyof T>;
8 new(...arguments_: Arguments): T;
9};
10
11/**
12Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
13
14@category Class
15*/
16export type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;
17
18/**
19Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes).
20
21@category Class
22
23@privateRemarks
24We cannot use a `type` here because TypeScript throws: 'abstract' modifier cannot appear on a type member. (1070)
25*/
26// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
27export interface AbstractClass<T, Arguments extends unknown[] = any[]> extends AbstractConstructor<T, Arguments> {
28 prototype: Pick<T, keyof T>;
29}
30
31/**
32Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
33
34@category Class
35*/
36export type AbstractConstructor<T, Arguments extends unknown[] = any[]> = abstract new(...arguments_: Arguments) => T;
37
38/**
39Matches a JSON object.
40
41This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`.
42
43@category JSON
44*/
45export type JsonObject = {[Key in string]: JsonValue} & {[Key in string]?: JsonValue | undefined};
46
47/**
48Matches a JSON array.
49
50@category JSON
51*/
52export type JsonArray = JsonValue[] | readonly JsonValue[];
53
54/**
55Matches any valid JSON primitive value.
56
57@category JSON
58*/
59export type JsonPrimitive = string | number | boolean | null;
60
61/**
62Matches any valid JSON value.
63
64@see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`.
65
66@category JSON
67*/
68export type JsonValue = JsonPrimitive | JsonObject | JsonArray;