enso
Version:
Maximalist state & form management library for React
123 lines • 6.64 kB
text/typescript
export declare namespace EnsoUtils {
/**
* Removes indexed fields leaving only statically defined.
*/
type PickIndexed<Payload> = {
[Key in keyof Payload as string extends Debrand<Key> ? Key : number extends Debrand<Key> ? Key : symbol extends Debrand<Key> ? Key : never]: Payload[Key];
};
type IndexedKeys<Payload> = keyof PickIndexed<Payload>;
/**
* Removes indexed fields leaving only statically defined.
*/
type PickStatic<Payload> = {
[Key in keyof Payload as string extends Debrand<Key> ? never : number extends Debrand<Key> ? never : symbol extends Debrand<Key> ? never : Key]: Payload[Key];
};
type StaticKeys<Payload> = keyof PickStatic<Payload>;
/**
* Resolves true if the given key is statically defined in the given type.
*/
type IsStaticKey<Type, Key extends keyof NonNullish<Type>> = Key extends StaticKeys<Type> ? true : false;
/**
* Resolves true if the passed key is a required field of the passed model.
*/
type IsRequiredKey<Payload, Key extends keyof Payload> = IsStaticKey<Payload, Key> extends true ? Partial<Pick<Payload, Key>> extends Pick<Payload, Key> ? false : true : false;
type PickOptional<Payload> = {
[Key in keyof Payload as IsStaticKey<Payload, Key> extends true ? Partial<Pick<Payload, Key>> extends Pick<Payload, Key> ? Key : never : never]: Payload[Key];
};
type OptionalKeys<Payload> = keyof PickOptional<Payload>;
type IsOptionalKey<Type, Key extends keyof NonNullish<Type>> = Key extends OptionalKeys<Type> ? true : false;
/**
* Nullish value type.
*/
type Nullish<Type = never> = Type | null | undefined;
type HasNullish<Type> = Not<IsNever<Extract<Type, Nullish>>>;
/**
* Excludes nulish values from the given type.
*/
type NonNullish<Type> = Exclude<Type, null | undefined>;
/**
* Excludes undefined values from the given type.
*/
type NonUndefined<Type> = Exclude<Type, undefined>;
/**
* Falsy value type.
*/
type Falsy = false | 0 | "" | Nullish;
type NonFalsy<Type> = Exclude<Type, Falsy>;
type Static<Class extends Interface & {
new (...args: any[]): any;
}, Interface> = InstanceType<Class>;
type NonTuple<Type> = Type extends Tuple ? never : Type;
type ArrayConstraint = unknown[] | ReadonlyArrayConstraint;
type StaticArray = Tuple;
type Tuple = [unknown, ...unknown[]] | readonly [unknown, ...unknown[]];
type ReadonlyArrayConstraint = readonly unknown[];
type IsReadonlyArray<Type> = Type extends readonly unknown[] ? Type extends unknown[] ? false : true : false;
type IndexOfTuple<Type extends Tuple> = Exclude<keyof Type, keyof any[]> extends infer Key ? Key extends `${infer Index extends number}` ? Index : never : never;
type Not<Type> = Type extends true ? false : true;
type And<Type1, Type2, Type3 = Type2, Type4 = Type3, Type5 = Type4, Type6 = Type5, Type7 = Type6, Type8 = Type7, Type9 = Type8, Type10 = Type9> = true extends Type1 & Type2 & Type3 & Type4 & Type5 & Type6 & Type7 & Type8 & Type9 & Type10 ? true : false;
type Or<Type1, Type2, Type3 = Type2, Type4 = Type3, Type5 = Type4, Type6 = Type5, Type7 = Type6, Type8 = Type7, Type9 = Type8, Type10 = Type9> = true extends Type1 | Type2 | Type3 | Type4 | Type5 | Type6 | Type7 | Type8 | Type9 | Type10 ? true : false;
type IfAnyOr<Type, IfType, OrType> = 0 extends 1 & Type ? IfType : OrType;
type AnyOr<Type, OrType> = IfAnyOr<Type, any, OrType>;
type IsAny<Type> = IfAnyOr<Type, true, false>;
type OnlyAny<Type> = IfAnyOr<Type, any, never>;
type IfUnknownOr<Type, IfType, OrType> = IfAnyOr<Type, OrType, [
Type
] extends [unknown] ? (unknown extends Type ? IfType : OrType) : OrType>;
type UnknownOr<Type, OrType> = IfUnknownOr<Type, unknown, OrType>;
type IsUnknown<Type> = IfUnknownOr<Type, true, false>;
type IsNotTop<Type> = Type extends {} ? true : false;
type ResolveTop<Type> = IsUnknown<Type> extends true ? never : any;
type IsNever<Type> = [Type] extends [never] ? true : false;
type NeverDefault<Type, DefaultType> = [Type] extends [never] ? DefaultType : Type;
type IsExtreme<Type> = Or<IsAny<Type>, IsUnknown<Type>, IsNever<Type>>;
type CovariantifyProperty<Type> = {
[Key in keyof Type]: Type[Key];
};
type CovariantifyKeyof<Type> = Exclude<keyof Type, keyof object> extends infer Key extends keyof Type ? Key : never;
type UnionToIntersection<Union> = (Union extends any ? (key: Union) => void : never) extends (key: infer Intersection) => void ? Intersection : never;
/**
* Resolves union keys.
*/
type UnionKeys<Type> = Type extends Type ? keyof Type : never;
/**
* Resolves union value.
*/
type UnionValue<Type, UnionKey extends UnionKeys<Type>> = Type extends {
[Key in UnionKey]: unknown;
} ? Type[UnionKey] : never;
type Primitive = string | number | boolean | undefined | null | symbol | bigint;
type BrandedPrimitive = Primitive & AnyBrand;
type Branded<Type, Symbol extends symbol> = Type & {
[Key in Symbol]: true;
};
/**
* Any brand type that can be mixed with string number or symbol to create
* opaque primitive.
*/
type AnyBrand = {
[key: keyof any]: any;
};
/**
* Removes brand from the given type.
*/
type Debrand<Type> = Type extends infer _Brand extends AnyBrand & (infer Debranded extends Primitive) ? Debranded : Type;
type StaticImplements<Class extends Interface & {
new (...args: any[]): any;
}, Interface> = InstanceType<Class>;
type NonObject = Primitive | BrandedPrimitive;
type HasNonObject<Type> = Not<IsNever<Extract<Type, BrandedPrimitive> | Exclude<Type, object>>>;
type OnlyObject<Type> = Extract<Exclude<Type, BrandedPrimitive>, object>;
type Transparent<Type> = Type extends infer Type ? {
[Key in keyof Type]: Type[Key];
} : never;
type Expose<Type> = Type & {};
type Extends<Qualifier, QualifierToCheck> = Extract<Qualifier, QualifierToCheck> extends infer RefQualifier ? Not<IsNever<RefQualifier>> : never;
namespace Union {
type Shared<Type> = Pick<Type, {
[Key in UnionKeys<Type>]: IsSharedKey<Type, Key> extends true ? Key : never;
}[UnionKeys<Type>]>;
type IsSharedKey<Type, Key extends keyof Type> = Type extends infer TypeUnion ? Type extends Type ? Key extends keyof TypeUnion ? TypeUnion[Key] extends Type[Key] ? true : false : false : never : never;
}
}
//# sourceMappingURL=utils.d.ts.map