inngest
Version:
Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.
1 lines • 12.2 kB
Source Map (JSON)
{"version":3,"file":"types.cjs","names":[],"sources":["../../src/helpers/types.ts"],"sourcesContent":["import type { EventPayload } from \"../types.ts\";\n\n/**\n * Returns the given generic as either itself or an array of itself.\n */\nexport type SingleOrArray<T> = T | T[];\n\n/**\n * Given type `T`, return it as an array if it is not already an array.\n */\n// biome-ignore lint/suspicious/noExplicitAny: intentional\nexport type AsArray<T> = T extends any[] ? T : [T];\n\n/**\n * Given type `T`, return a tuple of `T` that contains at least one element,\n * where `T` is not distributed, such that each array element could be any type\n * that satisfies `T`.\n *\n * See also {@link AsDistributedTuple}.\n *\n * @example\n * ```ts\n * // [\"foo\", ...\"foo\"[]]\n * type T0 = AsTuple<\"foo\">;\n *\n * // [\"foo\" | \"bar\", ...(\"foo\" | \"bar\")[]]\n * type T1 = AsTuple<\"foo\" | \"bar\">;\n */\nexport type AsTuple<T> = Simplify<[T, ...T[]]>;\n\n/**\n * Given type `T`, return a tuple of `T` that contains at least one element,\n * where `T` is also distributed, such that the array can be type narrowed by\n * checking a single element.\n *\n * See also {@link AsTuple}.\n *\n * @example\n * ```ts\n * // [\"foo\", ...\"foo\"[]]\n * type T0 = AsDistributedTuple<\"foo\">;\n *\n * // [\"foo\", ...\"foo\"[]] | [\"bar\", ...\"bar\"[]]\n * type T1 = AsDistributedTuple<\"foo\" | \"bar\">;\n * ```\n */\n// biome-ignore lint/suspicious/noExplicitAny: intentional\nexport type AsDistributedTuple<T> = T extends any ? [T, ...T[]] : never;\n\n/**\n * Returns the given generic as either itself or a promise of itself.\n */\nexport type MaybePromise<T> = T | Promise<T>;\n\n/**\n * Acts like `Partial<T>` but only for the keys in `K`, leaving the rest alone.\n */\nexport type PartialK<T, K extends PropertyKey = PropertyKey> = Partial<\n Pick<T, Extract<keyof T, K>>\n> &\n Omit<T, K> extends infer O\n ? { [P in keyof O]: O[P] }\n : never;\n\n/**\n * A payload that could be sent to Inngest.\n */\nexport type SendEventPayload = SingleOrArray<\n PartialK<Omit<EventPayload, \"v\">, \"ts\">\n>;\n\n/**\n * @public\n */\nexport type WithoutInternal<T extends Record<string, EventPayload>> = {\n [K in keyof T as WithoutInternalStr<K & string>]: T[K];\n};\n\nexport type WithoutInternalStr<T extends string> = T extends `inngest/${string}`\n ? never\n : T;\n\n/**\n * A list of simple, JSON-compatible, primitive types that contain no other\n * values.\n */\nexport type Primitive =\n | null\n | undefined\n | string\n | number\n | boolean\n | symbol\n | bigint;\n\n/**\n * Returns all keys from objects in the union `T`.\n *\n * @public\n */\nexport type UnionKeys<T> = T extends T ? keyof T : never;\n\n/**\n * Enforces strict union comformity by ensuring that all potential keys in a\n * union of objects are accounted for in every object.\n *\n * Requires two generics to be used, so is abstracted by {@link StrictUnion}.\n *\n * @public\n */\n// biome-ignore lint/suspicious/noExplicitAny: intentional\nexport type StrictUnionHelper<T, TAll> = T extends any\n ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>>\n : never;\n\n/**\n * Enforces strict union comformity by ensuring that all potential keys in a\n * union of objects are accounted for in every object.\n *\n * @public\n */\nexport type StrictUnion<T> = StrictUnionHelper<T, T>;\n\n/**\n * Returns `true` if the given generic `T` is a string literal, e.g. `\"foo\"`, or\n * `false` if it is a string type, e.g. `string`.\n *\n * Useful for checking whether the keys of an object are known or not.\n *\n * @example\n * ```ts\n * // false\n * type ObjIsGeneric = IsStringLiteral<keyof Record<string, boolean>>;\n *\n * // true\n * type ObjIsKnown = IsStringLiteral<keyof { foo: boolean; }>; // true\n * ```\n *\n * @internal\n */\nexport type IsStringLiteral<T extends string> = string extends T ? false : true;\n\n/**\n * Returns `true` if the given generic `T` is `any`, or `false` if it is not.\n */\nexport type IsAny<T> = 0 extends 1 & T ? true : false;\n\n/**\n * Given a function T, return the awaited return type of that function,\n * ignoring the fact that T may be undefined.\n */\n// biome-ignore lint/suspicious/noExplicitAny: intentional\nexport type Await<T extends ((...args: any[]) => any) | undefined> = Awaited<\n ReturnType<NonNullable<T>>\n>;\n\n/**\n * Given an object TAcc and an array of objects TArr, return a new object that\n * is the result of merging all of the objects in TArr into TAcc.\n */\nexport type ObjectAssign<TArr, TAcc = {}> = TArr extends [\n infer TFirst,\n ...infer TRest,\n]\n ? Simplify<ObjectAssign<TRest, Omit<TAcc, keyof TFirst> & TFirst>>\n : TAcc;\n\n/**\n * Make a type's keys mutually exclusive.\n *\n * @example\n * Make 1 key mutually exclusive with 1 other key.\n *\n * ```ts\n * type MyType = ExclusiveKeys<{a: number, b: number}, \"a\", \"b\">\n *\n * const valid1: MyType = { a: 1 }\n * const valid2: MyType = { b: 1 }\n * const invalid1: MyType = { a: 1, b: 1 }\n * ```\n *\n * @example\n * Make 1 key mutually exclusive with 2 other keys.\n *\n * ```ts\n * type MyType = ExclusiveKeys<{a: number, b: number, c: number}, \"a\", \"b\" | \"c\">\n *\n * const valid1: MyType = { a: 1 };\n * const valid2: MyType = { b: 1, c: 1 };\n * const invalid1: MyType = { a: 1, b: 1 };\n * const invalid2: MyType = { a: 1, c: 1 };\n * const invalid3: MyType = { a: 1, b: 1, c: 1 };\n * ```\n */\nexport type ExclusiveKeys<T, Keys1 extends keyof T, Keys2 extends keyof T> =\n | (Omit<T, Keys1> & { [K in Keys1]?: never })\n | (Omit<T, Keys2> & { [K in Keys2]?: never });\n\n/**\n * A type that represents either `A` or `B`. Shared properties retain their\n * types and unique properties are marked as optional.\n */\nexport type Either<A, B> = Partial<A> & Partial<B> & (A | B);\n\n/**\n * Given a function `T`, return the parameters of that function, except for the\n * first one.\n */\nexport type ParametersExceptFirst<T> = T extends (\n // biome-ignore lint/suspicious/noExplicitAny: intentional\n arg0: any,\n ...rest: infer U\n // biome-ignore lint/suspicious/noExplicitAny: intentional\n) => any\n ? U\n : never;\n\n/**\n * Given an object `T`, return `true` if it contains no keys, or `false` if it\n * contains any keys.\n *\n * Useful for detecting the passing of a `{}` (any non-nullish) type.\n */\nexport type IsEmptyObject<T> = {} extends T\n ? T extends {}\n ? true\n : false\n : false;\n\n/**\n * Create a tuple that can be of length 1 to `TLength`, where each element is\n * of type `TElement`.\n */\nexport type RecursiveTuple<\n TElement,\n TLength extends number,\n TAccumulator extends TElement[] = [TElement],\n> = TAccumulator[\"length\"] extends TLength\n ? TAccumulator\n :\n | RecursiveTuple<TElement, TLength, [TElement, ...TAccumulator]>\n | TAccumulator;\n\nexport type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};\n\nexport type ConditionalSimplifyDeep<\n Type,\n ExcludeType = never,\n IncludeType = unknown,\n> = Type extends ExcludeType\n ? Type\n : Type extends IncludeType\n ? {\n [TypeKey in keyof Type]: ConditionalSimplifyDeep<\n Type[TypeKey],\n ExcludeType,\n IncludeType\n >;\n }\n : Type;\n\nexport type SimplifyDeep<Type> = ConditionalSimplifyDeep<\n Type,\n Function | Iterable<unknown>,\n object\n>;\n\n/**\nReturns a boolean for whether the given type is `null`.\n*/\nexport type IsNull<T> = [T] extends [null] ? true : false;\n\n/**\nReturns a boolean for whether the given type is `unknown`.\n\n{@link https://github.com/dsherret/conditional-type-checks/pull/16}\n\nUseful in type utilities, such as when dealing with unknown data from API calls.\n\n@example\n```\nimport type {IsUnknown} from 'type-fest';\n\n// https://github.com/pajecawav/tiny-global-store/blob/master/src/index.ts\ntype Action<TState, TPayload = void> =\n\tIsUnknown<TPayload> extends true\n\t\t? (state: TState) => TState,\n\t\t: (state: TState, payload: TPayload) => TState;\n\nclass Store<TState> {\n\tconstructor(private state: TState) {}\n\n\texecute<TPayload = void>(action: Action<TState, TPayload>, payload?: TPayload): TState {\n\t\tthis.state = action(this.state, payload);\n\t\treturn this.state;\n\t}\n\n\t// ... other methods\n}\n\nconst store = new Store({value: 1});\ndeclare const someExternalData: unknown;\n\nstore.execute(state => ({value: state.value + 1}));\n//=> `TPayload` is `void`\n\nstore.execute((state, payload) => ({value: state.value + payload}), 5);\n//=> `TPayload` is `5`\n\nstore.execute((state, payload) => ({value: state.value + payload}), someExternalData);\n//=> Errors: `action` is `(state: TState) => TState`\n```\n*/\nexport type IsUnknown<T> = unknown extends T // `T` can be `unknown` or `any`\n ? IsNull<T> extends false // `any` can be `null`, but `unknown` can't be\n ? true\n : false\n : false;\n\n/**\nReturns a boolean for whether the two given types are equal.\n\n{@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650}\n{@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796}\n\nUse-cases:\n- If you want to make a conditional branch based on the result of a comparison of two types.\n\n@example\n```\nimport type {IsEqual} from 'type-fest';\n\n// This type returns a boolean for whether the given array includes the given item.\n// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.\ntype Includes<Value extends readonly any[], Item> =\n\tValue extends readonly [Value[0], ...infer rest]\n\t\t? IsEqual<Value[0], Item> extends true\n\t\t\t? true\n\t\t\t: Includes<rest, Item>\n\t\t: false;\n```\n*/\nexport type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <\n G,\n>() => G extends B ? 1 : 2\n ? true\n : false;\n\n/**\n * Returns a boolean for whether the given type `T` is `never`.\n */\nexport type IsNever<T> = [T] extends [never] ? true : false;\n\n/**\n * Given a type `T`, return `Then` if `T` is a string, number, or symbol\n * literal, else `Else`.\n *\n * `Then` defaults to `true` and `Else` defaults to `false`.\n *\n * Useful for determining if an object is a generic type or has known keys.\n *\n * @example\n * ```ts\n * type IsLiteralType = IsLiteral<\"foo\">; // true\n * type IsLiteralType = IsLiteral<string>; // false\n *\n * type IsLiteralType = IsLiteral<1>; // true\n * type IsLiteralType = IsLiteral<number>; // false\n *\n * type IsLiteralType = IsLiteral<symbol>; // true\n * type IsLiteralType = IsLiteral<typeof Symbol.iterator>; // false\n *\n * type T0 = { foo: string };\n * type HasAllKnownKeys = IsLiteral<keyof T0>; // true\n *\n * type T1 = { [x: string]: any; foo: boolean };\n * type HasAllKnownKeys = IsLiteral<keyof T1>; // false\n * ```\n */\nexport type IsLiteral<T, Then = true, Else = false> = string extends T\n ? Else\n : number extends T\n ? Else\n : symbol extends T\n ? Else\n : Then;\n\n/**\n * Given an object `T`, return the keys of that object that are known literals.\n *\n * Useful for filtering out generic mapped types from objects.\n *\n * @example\n * ```ts\n * type T0 = { foo: string };\n * type RegularKeys = keyof T0; // \"foo\"\n * type KnownKeys = KnownLiteralKeys<T0>; // \"foo\"\n *\n * type T1 = { [x: string]: any; foo: boolean };\n * type RegularKeys = keyof T1; // string | number\n * type KnownKeys = KnownLiteralKeys<T1>; // \"foo\"\n * ```\n */\nexport type KnownKeys<T> = keyof {\n [K in keyof T as IsLiteral<K, K, never>]: T[K];\n};\n\n/**\n * Given an object `T`, return the keys of that object that are public, ignoring\n * `private` and `protected` keys.\n *\n * This shouldn't commonly be used or exposed in user-facing types, as it can\n * skew extension checks.\n */\nexport type Public<T> = { [K in keyof T]: T[K] };\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n"],"mappings":";;AAgaA,SAAgB,SAAS,OAAkD;AACzE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM"}