import type es from 'estree'; import { type InvalidNumberParameterErrorOptions } from '../errors/rttcErrors'; import { RuntimeSourceError } from '../errors/base'; import { Chapter } from '../langs'; import type { Node, Value } from '../types'; import { TupleOfLength } from './typeUtils'; /** * Error type to be thrown by runtime type checking functions. This is usually caused by a user * trying to do something that would work in Javascript (like `'1' - 1`) but is forbidden * in Source. */ export declare class RuntimeTypeError extends RuntimeSourceError { readonly side: string; readonly expected: string; readonly got: string; readonly chapter: Chapter; constructor(node: Node, side: string, expected: string, got: string, chapter?: Chapter); explain(): string; } type TypeOfConstants = 'array' | 'bigint' | 'boolean' | 'function' | 'null' | 'number' | 'object' | 'regexp' | 'string' | 'undefined'; export type TypeOfConstantToType = T extends 'array' ? unknown[] : T extends 'bigint' ? bigint : T extends 'boolean' ? boolean : T extends 'function' ? (...args: any[]) => any : T extends 'null' ? null : T extends 'number' ? number : T extends 'object' ? object : T extends 'regexp' ? RegExp : T extends 'string' ? string : T extends 'undefined' ? undefined : never; /** * A wrapper around the typeof operator to account for `null` and arrays. */ export declare function typeOf(v: bigint): 'bigint'; export declare function typeOf(v: boolean): 'boolean'; export declare function typeOf(v: number): 'number'; export declare function typeOf(v: RegExp): 'regexp'; export declare function typeOf(v: string): 'string'; export declare function typeOf(v: (...args: any[]) => any): 'function'; export declare function typeOf(v: null): 'null'; export declare function typeOf(v: unknown[]): 'array'; export declare function typeOf(v: object): 'object'; export declare function typeOf(v: undefined): 'undefined'; export declare function typeOf(v: unknown): TypeOfConstants; /** * Returns `true` if the provided value is a `number`. */ export declare function isNumber(v: unknown): v is number; /** * Returns `true` is the provided value is a `number` and also a valid * index for an array.\ * See section 4 of https://2ality.com/2012/12/arrays.html */ export declare function isArrayIndex(v: unknown): v is number; export declare function isArray(v: unknown): v is unknown[]; export declare function isString(v: unknown): v is string; export declare function isBool(v: unknown): v is boolean; export declare function isObject(v: Value): v is object; /** * Checks that the given unary expression has a valid type, i.e `!` is used with booleans * and `-` and `+` are used with numbers. */ export declare function checkUnaryExpression(node: Node, operator: '!', value: unknown, chapter?: Chapter): asserts value is boolean; export declare function checkUnaryExpression(node: Node, operator: '-' | '+', value: unknown, chapter?: Chapter): asserts value is number; export declare function checkUnaryExpression(node: Node, operator: es.UnaryOperator, value: unknown, chapter?: Chapter): asserts value is number | boolean; /** * Checks that the given binary expression has a valid type: * 1. `+` can be used with both numbers or both strings * 2. `-`, `/`, `%`, `*`, `>`, `>=`, `<`, `<=` can only be used with numbers * 3. `||` and `&&` can only be used with booleans * 4. Equality operators can be used with any values */ export declare function checkBinaryExpression(node: Node, operator: '+', chapter: Chapter, values: [unknown, unknown]): asserts values is [string, string] | [number, number]; export declare function checkBinaryExpression(node: Node, operator: '-' | '*' | '/' | '<' | '<=' | '>' | '>=' | '%', chapter: Chapter, values: [unknown, unknown]): asserts values is [number, number]; export declare function checkBinaryExpression(node: Node, operator: '===' | '!==', chapter: Chapter.SOURCE_1 | Chapter.SOURCE_2, values: [unknown, unknown]): asserts values is [string, string] | [number, number]; export declare function checkBinaryExpression(node: Node, operator: '===' | '!==', chapter: Exclude, values: [unknown, unknown]): asserts values is [T, T]; export declare function checkBinaryExpression(node: Node, operator: es.BinaryOperator, chapter: Chapter, values: [unknown, unknown]): asserts values is [any, any]; /** * Checks that the given if statement's test has a boolean type */ export declare function checkIfStatement(node: Node, test: unknown, chapter?: Chapter): asserts test is boolean; export declare function checkoutofRange(node: Node, index: Value, chapter?: Chapter): void; /** * Check that the given MemberExpression `x[y]` is of the correct type: * 1. `x` is an array and `y` is a valid array index * 2. `x` is an object and `y` is a string */ export declare function checkMemberAccess(node: Node, args: [Value, Value]): asserts args is [object, string] | [unknown[], number]; export declare function checkArray(node: Node, maybeArray: Value, chapter?: Chapter): asserts maybeArray is unknown[]; type FunctionOfLength = (...args: TupleOfLength) => unknown; /** * Type guard for checking that the provided value is a function and that it has the specified number of parameters. * Of course at runtime parameter types are not checked, so this is only useful when combined with TypeScript types. */ export declare function isFunctionOfLength any>(f: (...args: any) => any, l: Parameters['length']): f is T; export declare function isFunctionOfLength(f: unknown, l: T): f is FunctionOfLength; /** * Assertion version of {@link isFunctionOfLength} * * @param f Value to validate * @param l Number of parameters that `f` is expected to have * @param func_name Function within which the validation is occurring * @param type_name Optional alias for the function type * @param param_name Name of the parameter that's being validated */ export declare function assertFunctionOfLength any>(f: (...args: any) => any, l: Parameters['length'], func_name: string, type_name?: string, param_name?: string): asserts f is T; export declare function assertFunctionOfLength(f: unknown, l: T, func_name: string, type_name?: string, param_name?: string): asserts f is FunctionOfLength; /** * Function for checking if the given `obj` is a tuple of the given length. Optionally, providing a type guard * function or string can type check the entire tuple. */ export declare function isTupleOfLength(obj: U[], l: T): obj is TupleOfLength; export declare function isTupleOfLength(obj: unknown, l: T, guard: (arg: unknown) => arg is U): obj is TupleOfLength; export declare function isTupleOfLength(obj: unknown, l: T, guard: U): obj is TupleOfLength>; export declare function isTupleOfLength(obj: unknown, l: T): obj is TupleOfLength; /** * Assertion version of {@link isTupleOfLength} */ export declare function assertTupleOfLength(obj: U[], l: T, func_name: string, param_name?: string): asserts obj is TupleOfLength; export declare function assertTupleOfLength(obj: unknown, l: T, func_name: string, param_name?: string): asserts obj is TupleOfLength; /** * Function for checking if a given value is a number and that it also potentially satisfies a bunch of other criteria: * - Within a given range of [min, max] * - Is an integer * - Is not NaN */ export declare function isNumberWithinRange(value: unknown, min?: number, max?: number, integer?: boolean): value is number; export declare function isNumberWithinRange(value: unknown, options: InvalidNumberParameterErrorOptions): value is number; export interface AssertNumberWithinRangeOptions extends InvalidNumberParameterErrorOptions { func_name: string; param_name?: string; } /** * Assertion version of {@link isNumberWithinRange} */ export declare function assertNumberWithinRange(value: unknown, func_name: string, min?: number, max?: number, integer?: boolean, param_name?: string): asserts value is number; export declare function assertNumberWithinRange(value: unknown, options: AssertNumberWithinRangeOptions): asserts value is number; export {};