import { Stringable, TemplateLiteralStringable, ToString } from '../types/String'; import { Constructor, Entries, Equals, HasTypedConstructor, Keys, OnlyInfer, ReplaceValuesRecursive } from '../types/misc'; import { Filter as TsToolbeltFilter } from 'ts-toolbelt/out/Object/Filter'; import { IntersectOf } from 'ts-toolbelt/out/Union/IntersectOf'; import { ListOf } from 'ts-toolbelt/out/Union/ListOf'; import { Replace } from 'ts-toolbelt/out/Union/Replace'; import { AnyKey } from 'tsdef'; /** * a function to be used when you need to provide a type in a value position. currently this is only supposed to be used * by {@link narrow} but there may be other use cases for it. * * you should not call this function, since you can specify the generic without calling it. * @example * as // correct * as() // incorrect * @throws */ export declare const as: (_dontCallThisFunction: never) => T; /** * narrows the given value from type `Base` to type `Narrowed` without having to assign it to a new variable * * due to limitations in generics and assertion functions, you have to provide the type using the {@link as} function. * @example * declare const foo: number * cast(foo, as<1 | 2>) * type Bar = typeof foo //1|2 */ export declare const narrow: <_ extends OnlyInfer, Base, Narrowed extends Base>(value: Base, type: typeof as) => asserts value is Narrowed; /** * unsafely narrows the given value to type `T` without having to assign it to a new variable. * * because of how assertion functions work, the type will narrow to `never` if the types don't overlap * @example * //safe example: * declare const foo: number * unsafeCast<1|2>(foo) * type Bar = typeof foo //1|2 * * //unsafe example: * declare const foo: string * unsafeCast<1|2>(foo) * type Bar = typeof foo //never */ export declare const unsafeNarrow: (_value: unknown) => asserts _value is T; /** * casts the given value from type `Original` to an itersection of `Original & Casted` (like type predicates & type assertions do), * but instead of changing the type of the original variable, it returns a value with the new type (like regular type casting does). * * due to limitations in generics and assertion functions, you have to provide the type using the {@link as} function. * * because of how assertion functions work, the type will narrow to `never` if the types don't overlap * @example * declare const foo: number * cast(foo, as<1 | 2>) * type Bar = typeof foo //1|2 */ export declare const narrowCast: <_ extends OnlyInfer, Original, Casted>(value: Original, _type: (_dontCallThisFunction: never) => Casted) => Original & Casted; /** * converts the given `value` to a string, preserving its value at compiletime where possible */ export declare const toStringType: { (value: T): ToString; (value: T): string; }; /** * asserts that a value matches the given type * * **WARNING**: for most type-testing scenarios, you probably want to use {@link exactly} instead, as it does an exact * match whereas this function only verifies that the value `extends` the given type */ export declare const assertType: <_Expected, _Actual extends _Expected>() => void; export declare const exactly: { /** * Used to check that two types are an exact match. Useful for testing types.
* Comes in three forms: * - type form: `exactly()` * - mixed form: `exactly()(value)`
* - value form: `exactly(expectedValue, actualValue)`
* Correctly checks `any` and `never`. *
* **WARNING:** there are several cases where this doesn't work properly, * see [these issues](https://github.com/DetachHead/ts-helpers/labels/type%20testing) * @see Equals * # Mixed Form * ## `exactly` function * ### generics * * - **`Expected`:** The expected type.
* * ## curried function * This is implemented as a higher order function to allow partial inference on the Expected type. * ### parameters * * * **`value`:** The value that will be checked. * * **`_this_parameter_will_be_expected_if_the_types_dont_match`:** An unused parameter used to cause a compile error * when the `Expected` and `Actual` types don't match. ***DO NOT*** specify this parameter if prompted to, as it * means your types don't match.
* @example * let a: 1 | 2 = 1; * exactly()(a); // error as `number` is not an exact match of `1 | 2` * exactly()(a as number); // no error * exactly<1 | 2>()(a); // no error */ (): <_ extends OnlyInfer, Actual>(value: Actual, ..._this_parameter_will_be_expected_if_the_types_dont_match: Equals extends true ? [] : [never]) => Actual; /** * Used to check that two types are an exact match. Useful for testing types
* Comes in three forms: * - type form: `exactly()` * - mixed form: `exactly()(value)`
* - value form: `exactly(expectedValue, actualValue)`
* Correctly checks `any` and `never`. *
* **WARNING:** there are several cases where this doesn't work properly, * see [these issues](https://github.com/DetachHead/ts-helpers/labels/type%20testing) * # Type form * ## generics * - **`Expected`:** The expected type. * - **`Actual`:** The actual type * - **`_Bound`:** Used to bind the two types together, ***DO NOT*** specify this parameter.
* @example * type Foo = 1 | 2; * exactly<1, Foo>(); // error as `1 | 2` is not an exact match of `1` * exactly<1 | 2, Foo>(); // no error */ extends true ? Actual : never, Actual extends _Bound, _Bound = Expected>(): unknown; /** * Used to check that two types are an exact match. Useful for testing types
* Comes in three forms: * - type form: `exactly()` * - mixed form: `exactly()(value)`
* - value form: `exactly(expectedValue, actualValue)`
* Correctly checks `any` and `never`. *
* **WARNING:** there are several cases where this doesn't work properly, * see [these issues](https://github.com/DetachHead/ts-helpers/labels/type%20testing) * # value form * checks that the values match at runtime as well! * @param expected the expected value. * @param actual the actual value. * @example * declare const foo: 1 | 2; * exactly(foo, 1); // error as `1 | 2` is not an exact match of `1` * exactly(foo, 1 as 1 | 2); // no error */ <_ extends OnlyInfer, const Expected extends Equals extends true ? Actual : never, const Actual extends Bound, Bound = Expected>(expected: Expected, actual: Actual): void; }; /** * throws an error stating that the operation is not yet implemented. useful for shutting up the type checker * during development */ export declare const TODO: (reason?: string) => never; /** * returns whether the given object has the given propertyName and narrows the type of the object to either: * - the specified generic (if provided) * - OR adds the specified key to the type, if a generic is not provided */ export declare const hasPropertyPredicate: { /** * returns whether the given object has the given propertyName and narrows the type of the object to add the * specified key to the type */ (object: unknown, propertyName: Key): object is string extends Key ? unknown : { [K in IntersectOf & Key]: unknown; }; /** * returns whether the given object has the given propertyName and narrows the type of the object to the specified generic */ (object: unknown, propertyName: keyof Narrowed): object is Narrowed; }; /** * Object.entries but preserves the key types * * this is probably not 100% safe in some edge cases (see the issue linked below), but it's not worth the headaches that * treating all keys as `string[]` causes * @see https://github.com/Microsoft/TypeScript/issues/12870 */ export declare const entries: (object: T) => Entries; /** * runs the given `callback` until it returns `true` or `timeoutMs` is reached */ export declare const runUntil: (callback: () => Promise, timeoutMs: number) => Promise; export declare const isNullOrUndefined: (value: T) => (undefined extends T ? true : null extends T ? true : never) | (T extends null | undefined ? true : false); /** * wraps a boolean expression and does nothing with it. useful if you want to prevent an expression from narrowing * the type of a value for some reason (eg. to work around an incorrect type definition) */ export declare const dontNarrow: (expression: boolean) => boolean; /** * instantiates a class with a typed `constructor` property that returns the type of the class instead of {@link Function} * * **WARNING:** does not work properly with overloaded constructors * @see https://github.com/microsoft/TypeScript/issues/3841 */ export declare const New: (class_: T, ...args: ConstructorParameters) => HasTypedConstructor; declare const replacedUndefined: unique symbol; /** * recursively removes `undefined` properties from an object type. used by the `optionalProperties` function * only useful when using the `exactOptionalPropertyTypes` compiler option. this type has no effect if it's disabled * @example * type Foo = RemoveUndefinedPropertiesRecursive<{ a?: number | undefined, b: string | undefined }> // { a?: number, b: string | undefined } */ type OptionalProperties = TsToolbeltFilter<{ [K in keyof T]: ListOf extends infer Union ? { [UnionIndex in keyof Union]: Union[UnionIndex] extends object ? OptionalProperties : Replace; }[Keys] : never; }, typeof replacedUndefined, '<-contains'> & ReplaceValuesRecursive; /** * recursively removes `undefined` properties from an object. * useful when using the `exactOptionalPropertyTypes` compiler option */ export declare const optionalProperties: (object: T) => import("ts-toolbelt/out/Object/Pick").Pick<{ [K in keyof T]: ListOf extends infer Union ? { [UnionIndex in keyof Union]: Union[UnionIndex] extends object ? import("ts-toolbelt/out/Object/Pick").Pick extends infer Union ? any[Exclude, number>] : never; } : never, import("ts-toolbelt/out/Object/FilterKeys").FilterKeys extends infer Union ? any[Exclude, number>] : never; } : never, typeof replacedUndefined, "<-contains">> & ReplaceValuesRecursive : Replace; }[Exclude, number>] : never; }, import("ts-toolbelt/out/Object/FilterKeys").FilterKeys<{ [K in keyof T]: ListOf extends infer Union ? { [UnionIndex in keyof Union]: Union[UnionIndex] extends object ? import("ts-toolbelt/out/Object/Pick").Pick extends infer Union ? any[Exclude, number>] : never; } : never, import("ts-toolbelt/out/Object/FilterKeys").FilterKeys extends infer Union ? any[Exclude, number>] : never; } : never, typeof replacedUndefined, "<-contains">> & ReplaceValuesRecursive : Replace; }[Exclude, number>] : never; }, typeof replacedUndefined, "<-contains">> & ReplaceValuesRecursive; export {}; //# sourceMappingURL=misc.d.ts.map