declare const invariantBrand: unique symbol; /** Create an [invariant type](https://basarat.gitbook.io/typescript/type-system/type-compatibility#footnote-invariance), which is a type that does not accept supertypes and subtypes. Use-case: - Prevent runtime errors that may occur due to assigning subtypes to supertypes. - Improve type signature of object methods like [`Object.keys()` or `Object.entries()`](https://github.com/microsoft/TypeScript/pull/12253#issuecomment-263132208) by sealing the object type. @example ``` import type {InvariantOf} from 'type-fest'; class Animal { constructor(public name: string) {} } class Cat extends Animal { meow() { // do something } } let animalArray: Animal[] = [new Animal('jerry')]; const catArray: Cat[] = [new Cat('tom')]; animalArray = catArray; // Okay if covariant animalArray.push(new Animal('another animal')); // Pushed an animal into catArray for (const c of catArray) { c.meow(); } // Allowed but, error at runtime let invariantAnimalArray: Array> = [new Animal('jerry')] as Array>; const invariantCatArray: Array> = [new Cat('tom')] as Array>; // @ts-expect-error invariantAnimalArray = invariantCatArray; // Error: Type 'InvariantOf[]' is not assignable to type 'InvariantOf[]'. ``` @example ``` import type {InvariantOf} from 'type-fest'; // In covariance (default) type FooBar = { foo: number; bar: string; }; type FooBarBaz = { baz: boolean; } & FooBar; declare const fooBar: FooBar; declare const fooBarBaz: FooBarBaz; function keyOfFooBar(fooBar: FooBar) { return Object.keys(fooBar) as Array; } keyOfFooBar(fooBar); //=> (keyof FooBar)[] keyOfFooBar(fooBarBaz); //=> (keyof FooBar)[] but, (keyof FooBarBaz)[] at runtime // In invariance export function invariantOf(value: Type): InvariantOf { return value as InvariantOf; } function keyOfInvariantFooBar(fooBar: InvariantOf) { return Object.keys(fooBar) as Array; } keyOfInvariantFooBar(invariantOf(fooBar)); // (keyof FooBar)[] // @ts-expect-error keyOfInvariantFooBar(invariantOf(fooBarBaz)); // Error: Argument of type 'InvariantOf' is not assignable to parameter of type 'InvariantOf'. ``` @category Type */ export type InvariantOf = Type & {[invariantBrand]: (_: Type) => Type}; export {};