@omnia/fx
Version:
Provide Omnia Fx typings and tooling for clientside Omnia development.
22 lines (21 loc) • 1.68 kB
TypeScript
export type PropPathCheckTargetType<T, Path extends string, TargetType> = string extends Path ? never : Path extends keyof T ? (T[Path] extends TargetType ? `${Path}` : never) : Path extends `${infer K}.${infer R}` ? K extends keyof T ? PropStartPathCheckTargetType<T[K], R, Path, TargetType> : never : never;
type PropStartPathCheckTargetType<T, Path extends string, StartPath extends string, TargetType> = string extends Path ? never : Path extends keyof T ? (T[Path] extends TargetType ? `${StartPath}` : never) : Path extends `${infer K}.${infer R}` ? K extends keyof T ? PropStartPathCheckTargetType<T[K], R, Path, TargetType> : never : never;
/**
* Returns the property path as string if valid for type, else compile error (Argument of type string is not assignable to never).
*
* Option: You can also make sure the path has a desired type propertyPath<,TargetType>
*
* Example:
* propertyPath<ISomeInterfaceOrType>()('PropertyName1.SomeSubProperty.SomeSubSubProperty'), note: propertyPath<ISomeInterfaceOrType>()
*
* or
*
* propertyPath<ISomeInterfaceOrType,IPropertyPathTargetType>()('PropertyName1.SomeSubProperty.SomeSubSubProperty'), this will ensure that the IPropertyPathTargetType is the TargetType
* for the specified path
*
* Gives compile error if property path is not valid for type StartType, i.e. a type safe way to handle property paths as string values
* @param name
* @returns {string} 'PropertyName1.SomeSubProperty.SomeSubSubProperty' as string
*/
export declare const propertyPath: <StartType, TargetType = any>() => <Path extends string>(propertyPathToCheck: PropPathCheckTargetType<StartType, Path, TargetType>) => string;
export {};