Doubter - v5.1.1-next.e4e15ab
    Preparing search index...

    Class FunctionShape<ArgsShape, ReturnShape, ThisShape>

    The shape of a function.

    Type Parameters

    • ArgsShape extends Shape<readonly any[], readonly any[]>

      The shape of the array of arguments.

    • ReturnShape extends AnyShape | null

      The return value shape, or null if unconstrained.

    • ThisShape extends AnyShape | null

      The shape of this argument, or null if unconstrained.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    _options: undefined | IssueOptions | Message

    The type issue options or the type issue message.

    _parseOptions: undefined | ParseOptions

    Parsing options that are used by a wrapper.

    annotations: Dict = {}

    The dictionary of shape annotations.

    argsShape: ArgsShape

    The shape of the array of arguments.

    inputs: readonly unknown[]

    The array of unique input types and values that are accepted by the shape.

    isAsync: boolean

    true if the shape allows only Shape.parseAsync and throws an error if Shape.parse is called, or false if the shape can be used in both sync and async contexts.

    isStrict: boolean = false

    true if input functions are wrapped during parsing to ensure runtime signature type-safety, or false otherwise.

    operations: readonly Operation[] = []

    The array of operations that are applied to the shape output.

    returnShape: ReturnShape

    The return value shape, or null if unconstrained.

    thisShape: ThisShape

    The shape of this value, or null if unconstrained.

    Methods

    • Synchronously parses the input.

      Parameters

      • input: any

        The shape input to parse.

      • options: ParseOptions

        Parsing options.

      • _nonce: number

        The globally unique number that identifies the parsing process.

      Returns Result<
          (
              this: InferOrDefault<ThisShape, typeof INPUT>,
              ...args: Input<ArgsShape>,
          ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
      >

      null if input matches the output, Ok that wraps the output, or an array of captured issues.

    • Asynchronously parses the input.

      Parameters

      • input: any

        The shape input to parse.

      • options: ParseOptions

        Parsing options.

      • _nonce: number

      Returns Promise<
          Result<
              (
                  this: InferOrDefault<ThisShape, typeof INPUT>,
                  ...args: Input<ArgsShape>,
              ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
          >,
      >

      null if input matches the output, Ok that wraps the output, or an array of captured issues.

    • Returns input types and literal values that this shape can accept as an input.

      Returns readonly unknown[]

    • Must return true if the shape must be used in async context only, otherwise the shape can be used in both sync and async contexts. Override this method to implement a custom shape.

      Returns boolean

    • Returns true if the shape accepts given input type or value, or false otherwise.

      Parameters

      • input: unknown

        The type or value that must be checked.

      Returns boolean

    • Adds an asynchronous operation to the shape.

      Type Parameters

      • Param

        The param that is passed to the operation when it is applied.

      Parameters

      • cb: OperationCallback<
            PromiseLike<
                Result<
                    (
                        this: InferOrDefault<ThisShape, typeof INPUT>,
                        ...args: Input<ArgsShape>,
                    ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
                >,
            >,
            (
                this: InferOrDefault<ThisShape, typeof INPUT>,
                ...args: Input<ArgsShape>,
            ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
            Param,
        >

        The callback that asynchronously applies an operation to the shape output value.

      • Optionaloptions: ParameterizedOperationOptions<Param>

        The operation options.

      Returns this

      The clone of the shape.

    • Adds an asynchronous operation to the shape.

      Parameters

      • cb: OperationCallback<
            PromiseLike<
                Result<
                    (
                        this: InferOrDefault<ThisShape, typeof INPUT>,
                        ...args: Input<ArgsShape>,
                    ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
                >,
            >,
            (
                this: InferOrDefault<ThisShape, typeof INPUT>,
                ...args: Input<ArgsShape>,
            ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
        >

        The callback that asynchronously applies an operation to the shape output value.

      • Optionaloptions: OperationOptions

        The operation options.

      Returns this

      The clone of the shape.

    • Adds a synchronous operation to the shape.

      Type Parameters

      • Param

        The param that is passed to the operation when it is applied.

      Parameters

      Returns this

      The clone of the shape.

    • Adds a synchronous operation to the shape.

      Parameters

      • cb: OperationCallback<
            Result<
                (
                    this: InferOrDefault<ThisShape, typeof INPUT>,
                    ...args: Input<ArgsShape>,
                ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
            >,
            (
                this: InferOrDefault<ThisShape, typeof INPUT>,
                ...args: Input<ArgsShape>,
            ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
        >

        The callback that synchronously applies an operation to the shape output value.

      • Optionaloptions: OperationOptions

        The operation options.

      Returns this

      The clone of the shape.

    • Adds annotations to the shape.

      Parameters

      • annotations: ReadonlyDict

        Annotations to add.

      Returns this

      The clone of the shape with the updated annotations.

    • Returns a sub-shape that describes a value associated with the given property name, or null if there's no such sub-shape.

      Parameters

      • _key: unknown

        The key for which the sub-shape must be retrieved.

      Returns null | AnyShape

      The sub-shape or null if there's no such key in the shape.

    • Creates a function that ensures that fn receives arguments and this value that conform the argsShape and the thisShape respectively, and synchronously returns the value that conforms the returnShape.

      Type Parameters

      • F extends (
            this: InferOrDefault<ThisShape, typeof OUTPUT>,
            ...args: Output<ArgsShape>,
        ) => InferOrDefault<ReturnShape, typeof INPUT>

        The function to which signature must be ensured.

      Parameters

      • fn: F

        The underlying function.

      • Optionaloptions: ParseOptions

        Parsing options. By default, options provided to the strict method are used.

      Returns (
          this: InferOrDefault<ThisShape, typeof INPUT, ThisType<F>>,
          ...args: Input<ArgsShape>,
      ) => InferOrDefault<ReturnShape, typeof OUTPUT, ReturnType<F>>

      The wrapper function.

    • Synchronously parses the value.

      Parameters

      • input: unknown

        The value to parse.

      • Optionaloptions: ParseOptions

        Parsing options.

      Returns (
          this: InferOrDefault<ThisShape, typeof INPUT>,
          ...args: Input<ArgsShape>,
      ) => InferOrDefault<ReturnShape, typeof OUTPUT>

      The value that conforms the output type of the shape.

      Error if the shape doesn't support the sync parsing, see Shape.isAsync.

      ValidationError if any issues occur during parsing.

    • Asynchronously parses the value.

      Parameters

      • input: unknown

        The value to parse.

      • Optionaloptions: ParseOptions

        Parsing options.

      Returns Promisify<
          (
              this: InferOrDefault<ThisShape, typeof INPUT>,
              ...args: Input<ArgsShape>,
          ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
      >

      The promise that resolves with the value that conforms the output type of the shape, or rejects with a ValidationError if any issues occur during parsing.

    • Synchronously parses the value and returns undefined if parsing fails.

      Parameters

      • input: unknown

        The value to parse.

      Returns
          | undefined
          | (
              (
                  this: InferOrDefault<ThisShape, typeof INPUT>,
                  ...args: Input<ArgsShape>,
              ) => InferOrDefault<ReturnShape, typeof OUTPUT>
          )

      The value that conforms the output type of the shape.

      Error if the shape doesn't support the sync parsing, see Shape.isAsync.

    • Synchronously parses the value and returns the default value if parsing fails.

      Type Parameters

      • DefaultValue

        The default value that is returned if parsing fails.

      Parameters

      • input: unknown

        The value to parse.

      • defaultValue: DefaultValue

        The default value that is returned if parsing fails.

      • Optionaloptions: ParseOptions

        Parsing options.

      Returns
          | (
              (
                  this: InferOrDefault<ThisShape, typeof INPUT>,
                  ...args: Input<ArgsShape>,
              ) => InferOrDefault<ReturnShape, typeof OUTPUT>
          )
          | DefaultValue

      The value that conforms the output type of the shape.

      Error if the shape doesn't support the sync parsing, see Shape.isAsync.

    • Asynchronously parses the value and returns undefined value if parsing fails.

      Parameters

      • input: unknown

        The value to parse.

      Returns Promisify<
          | undefined
          | (
              (
                  this: InferOrDefault<ThisShape, typeof INPUT>,
                  ...args: Input<ArgsShape>,
              ) => InferOrDefault<ReturnShape, typeof OUTPUT>
          ),
      >

      The value that conforms the output type of the shape.

    • Asynchronously parses the value and returns the default value if parsing fails.

      Type Parameters

      • DefaultValue

        The default value that is returned if parsing fails.

      Parameters

      • input: unknown

        The value to parse.

      • defaultValue: DefaultValue

        The default value that is returned if parsing fails.

      • Optionaloptions: ParseOptions

        Parsing options.

      Returns Promisify<
          | (
              (
                  this: InferOrDefault<ThisShape, typeof INPUT>,
                  ...args: Input<ArgsShape>,
              ) => InferOrDefault<ReturnShape, typeof OUTPUT>
          )
          | DefaultValue,
      >

      The value that conforms the output type of the shape.

    • Enables input function wrapping during parsing to ensure runtime signature type-safety. Wrapper ensures that input function receives arguments and this values that conform the argsShape and thisShape respectively, and returns the value that conforms returnShape.

      Parameters

      • Optionaloptions: ParseOptions

        Options that are used by the wrapper. If omitted then default options are applied: no early-return, no type coercion.

      Returns this

      The new function shape.

    • Synchronously parses the value and returns Ok or Err object that wraps the result.

      Parameters

      • input: unknown

        The value to parse.

      • Optionaloptions: ParseOptions

        Parsing options.

      Returns
          | Err
          | Ok<
              (
                  this: InferOrDefault<ThisShape, typeof INPUT>,
                  ...args: Input<ArgsShape>,
              ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
          >

      The Ok instance if parsing has succeeded or Err if parsing has failed.

      Error if the shape doesn't support the sync parsing, see Shape.isAsync.

    • Asynchronously parses the value and returns Ok or Err object that wraps the result.

      Parameters

      • input: unknown

        The value to parse.

      • Optionaloptions: ParseOptions

        Parsing options.

      Returns Promise<
          | Err
          | Ok<
              (
                  this: InferOrDefault<ThisShape, typeof INPUT>,
                  ...args: Input<ArgsShape>,
              ) => InferOrDefault<ReturnShape, typeof OUTPUT>,
          >,
      >

      The Ok instance if parsing has succeeded or Err if parsing has failed.

    Accessors

    • get isAsyncFunction(): boolean

      true if some shapes that describe the function signature are async, or false otherwise.

      Returns boolean