|
| 1 | +declare module "sap/ui/model/json/TypedJSONModel" { |
| 2 | + import JSONModel from "sap/ui/model/json/JSONModel"; |
| 3 | + import TypedJSONContext from "sap/ui/model/json/TypedJSONContext"; |
| 4 | + import Context from "sap/ui/model/Context"; |
| 5 | + |
| 6 | + /** |
| 7 | + * TypedJSONModel is a subclass of JSONModel that provides type-safe access to the model data. It is only available when using UI5 with TypeScript. |
| 8 | + * |
| 9 | + * @since 1.140.0 |
| 10 | + */ |
| 11 | + export default class TypedJSONModel<Data extends object> extends JSONModel { |
| 12 | + constructor(oData?: Data, bObserve?: boolean); |
| 13 | + createBindingContext<Path extends AbsoluteBindingPath<Data>>( |
| 14 | + sPath: Path, |
| 15 | + oContext?: Context, |
| 16 | + mParameters?: object, |
| 17 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type |
| 18 | + fnCallBack?: Function, |
| 19 | + bReload?: boolean, |
| 20 | + ): TypedJSONContext<Data, Path>; |
| 21 | + getData(): Data; |
| 22 | + getProperty<Path extends AbsoluteBindingPath<Data>>( |
| 23 | + sPath: Path, |
| 24 | + ): PropertyByAbsoluteBindingPath<Data, Path>; |
| 25 | + getProperty< |
| 26 | + Path extends RelativeBindingPath<Data, Root>, |
| 27 | + Root extends AbsoluteBindingPath<Data>, |
| 28 | + >( |
| 29 | + sPath: Path, |
| 30 | + oContext: TypedJSONContext<Data, Root>, |
| 31 | + ): PropertyByRelativeBindingPath<Data, Root, Path>; |
| 32 | + |
| 33 | + setData(oData: Data, bMerge?: boolean): void; |
| 34 | + |
| 35 | + // setProperty with AbsoluteBindingPath (context === undefined), |
| 36 | + // PLEASE NOTE: the parameter is still necessary so |
| 37 | + // the bAsyncUpdate parameter can also be used with absolute paths. |
| 38 | + setProperty<Path extends AbsoluteBindingPath<Data>>( |
| 39 | + sPath: Path, |
| 40 | + oValue: PropertyByAbsoluteBindingPath<Data, Path>, |
| 41 | + oContext?: undefined, |
| 42 | + bAsyncUpdate?: boolean, |
| 43 | + ): boolean; |
| 44 | + setProperty< |
| 45 | + Path extends RelativeBindingPath<Data, Root>, |
| 46 | + Root extends AbsoluteBindingPath<Data>, |
| 47 | + >( |
| 48 | + sPath: Path, |
| 49 | + oValue: PropertyByRelativeBindingPath<Data, Root, Path>, |
| 50 | + oContext: TypedJSONContext<Data, Root>, |
| 51 | + bAsyncUpdate?: boolean, |
| 52 | + ): boolean; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Valid absolute binding in a JSONModel with the underlying type `Type`. |
| 57 | + * Counterpart to {@link PropertyByAbsoluteBindingPath} |
| 58 | + * @example |
| 59 | + * type Person = { name: string, id: number }; |
| 60 | + * type PathInPerson = PathInJSONModel<Person>; // "/name" | "/id" |
| 61 | + * let path: PathInPerson = "/name"; // ok |
| 62 | + * path = "/firstName"; // error |
| 63 | + */ |
| 64 | + export type AbsoluteBindingPath<Type> = |
| 65 | + Type extends Array<unknown> |
| 66 | + ? // if Type is an array: |
| 67 | + | `/${number}` // /0 -> first element of array |
| 68 | + | `/${number}${AbsoluteBindingPath<Type[number]>}` // /0/{NestedPath} |
| 69 | + : // if Type is not an array: |
| 70 | + Type extends object |
| 71 | + ? |
| 72 | + | { |
| 73 | + [Key in keyof Type]: Type[Key] extends Array<unknown> |
| 74 | + ? // Type[Key] is an array: |
| 75 | + | `/${string & Key}/${number}` // items/0 -> elem of array |
| 76 | + // path can end there or: |
| 77 | + | `/${string & Key}/${number}${AbsoluteBindingPath<Type[Key][number]>}` // items/0/{NestedPath} |
| 78 | + : // Type[Key] is NOT an array: |
| 79 | + `/${string & Key}${AbsoluteBindingPath<Type[Key]>}`; |
| 80 | + }[keyof Type] |
| 81 | + | `/${string & PropertiesOf<Type>}` // /items/0/id -> last part of path |
| 82 | + : // if T is not of type object: |
| 83 | + never; |
| 84 | + |
| 85 | + /** |
| 86 | + * Valid relative binding path in a JSONModel. |
| 87 | + * The root of the path is defined by the given root string. |
| 88 | + * |
| 89 | + * @example |
| 90 | + * type PersonWrapper = { person: { name: string, id: number } }; |
| 91 | + * type PathRelativeToPerson = RelativeBindingPath<PersonWrapper, "/person">; // "name" | "id" |
| 92 | + */ |
| 93 | + export type RelativeBindingPath< |
| 94 | + Type, |
| 95 | + Root extends AbsoluteBindingPath<Type>, |
| 96 | + > = |
| 97 | + AbsoluteBindingPath<TypeAtPath<Type, Root>> extends `/${infer Rest}` |
| 98 | + ? Rest |
| 99 | + : never; |
| 100 | + |
| 101 | + /** |
| 102 | + * The type of a property in a JSONModel identified by the given path. |
| 103 | + * Counterpart to {@link AbsoluteBindingPath}. |
| 104 | + * @example |
| 105 | + * type Person = { name: string, id: number }; |
| 106 | + * type PersonName = PropertyInJSONModel<Person, "/name">; // string |
| 107 | + * const name: PersonName = "John"; // ok |
| 108 | + */ |
| 109 | + export type PropertyByAbsoluteBindingPath< |
| 110 | + Type, |
| 111 | + Path extends string, |
| 112 | + > = Path extends `/${number}` |
| 113 | + ? Type extends Array<infer U> |
| 114 | + ? U |
| 115 | + : never |
| 116 | + : Path extends `/${number}${infer Rest}` |
| 117 | + ? Type extends Array<infer U> |
| 118 | + ? PropertyByAbsoluteBindingPath<U, Rest> |
| 119 | + : never |
| 120 | + : Path extends `/${infer Key}/${number}/${infer Rest}` |
| 121 | + ? Key extends keyof Type |
| 122 | + ? FromArrayWithSubPath<Type, Key, Rest> |
| 123 | + : never |
| 124 | + : Path extends `/${infer Key}/${number}` |
| 125 | + ? Key extends keyof Type |
| 126 | + ? FromArrayElement<Type, Key> |
| 127 | + : never |
| 128 | + : Path extends `/${infer Key}/${infer Rest}` |
| 129 | + ? Key extends keyof Type |
| 130 | + ? FromNestedProperty<Type, Key, Rest> |
| 131 | + : never |
| 132 | + : Path extends `/${infer Key}` |
| 133 | + ? Key extends keyof Type |
| 134 | + ? FromTopLevelProperty<Type, Key> |
| 135 | + : never |
| 136 | + : never; |
| 137 | + |
| 138 | + /** |
| 139 | + * The type of a property in a JSONModel identified by the given relative path and root. |
| 140 | + * Counterpart to {@link RelativeBindingPath}. |
| 141 | + * @example |
| 142 | + * type PersonWrapper = { person: { name: string, id: number } }; |
| 143 | + * type PersonName = PropertyByRelativeBindingPath<PersonWrapper, "/person", "name">; |
| 144 | + * const name: PersonName = "John"; // ok |
| 145 | + */ |
| 146 | + export type PropertyByRelativeBindingPath< |
| 147 | + Type, |
| 148 | + Root extends string, |
| 149 | + RelativePath extends string, |
| 150 | + > = PropertyByAbsoluteBindingPath<Type, `${Root}/${RelativePath}`>; |
| 151 | + |
| 152 | + /*********************************************************************************************************************** |
| 153 | + * Helper types to split the types above into separate parts |
| 154 | + * to make it easier to read and understand. |
| 155 | + /**********************************************************************************************************************/ |
| 156 | + |
| 157 | + /** |
| 158 | + * Helper type to handle paths that point to an array with a subpath. |
| 159 | + * @example const path = "/orders/0/items" |
| 160 | + */ |
| 161 | + type FromArrayWithSubPath<Type, Key extends keyof Type, Rest extends string> = |
| 162 | + Type[Key] extends Array<infer U> |
| 163 | + ? PropertyByAbsoluteBindingPath<U, `/${Rest}`> |
| 164 | + : never; |
| 165 | + |
| 166 | + /** |
| 167 | + * Helper type to handle paths that point to an array element. |
| 168 | + * @example const path = "/orders/0" |
| 169 | + */ |
| 170 | + type FromArrayElement<Type, Key extends keyof Type> = |
| 171 | + Type[Key] extends Array<infer U> ? U : never; |
| 172 | + |
| 173 | + /** |
| 174 | + * Helper type to handle paths that point to a nested property. |
| 175 | + * @example const path = "/customer/address/street" |
| 176 | + */ |
| 177 | + type FromNestedProperty< |
| 178 | + Type, |
| 179 | + Key extends keyof Type, |
| 180 | + Rest extends string, |
| 181 | + > = PropertyByAbsoluteBindingPath<Type[Key], `/${Rest}`>; |
| 182 | + |
| 183 | + /** |
| 184 | + * Helper type to handle paths that point to a top-level property. |
| 185 | + * @example const path = "/customer" |
| 186 | + */ |
| 187 | + type FromTopLevelProperty<Type, Key extends keyof Type> = Type[Key]; |
| 188 | + |
| 189 | + /** |
| 190 | + * Helper type to navigate along a nested path. |
| 191 | + * Navigates from the root type along the path to determine the sub-type. |
| 192 | + */ |
| 193 | + type TypeAtPath< |
| 194 | + Type, |
| 195 | + Path extends string, |
| 196 | + > = Path extends `/${infer Key}/${infer Rest}` |
| 197 | + ? Key extends keyof Type |
| 198 | + ? TypeAtPath<Type[Key], `/${Rest}`> |
| 199 | + : never |
| 200 | + : Path extends `/${infer Key}` |
| 201 | + ? Key extends keyof Type |
| 202 | + ? Type[Key] |
| 203 | + : never |
| 204 | + : never; |
| 205 | + |
| 206 | + /** |
| 207 | + * Helper type to extract the names of the properties of a given type. |
| 208 | + * Excludes properties that are of the type `Function` or `symbol`. |
| 209 | + * @example |
| 210 | + * type Person = { name: string, id: number }; |
| 211 | + * type PersonProperties = PropertiesOf<Person>; // "name" | "id" |
| 212 | + * let property: PersonProperties = "name"; // ok |
| 213 | + * property = "firstName"; // error |
| 214 | + */ |
| 215 | + type PropertiesOf<Type> = { |
| 216 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type |
| 217 | + [Key in keyof Type]: Type[Key] extends Function |
| 218 | + ? never |
| 219 | + : Type[Key] extends symbol |
| 220 | + ? never |
| 221 | + : Key; |
| 222 | + }[keyof Type]; |
| 223 | + |
| 224 | + export {}; // this prevents the non-exported types like PropertiesOf from being visible for applications |
| 225 | +} |
| 226 | + |
| 227 | +declare module "sap/ui/model/json/TypedJSONContext" { |
| 228 | + import Context from "sap/ui/model/Context"; |
| 229 | + import TypedJSONModel from "sap/ui/model/json/TypedJSONModel"; |
| 230 | + import { |
| 231 | + AbsoluteBindingPath, |
| 232 | + RelativeBindingPath, |
| 233 | + PropertyByRelativeBindingPath, |
| 234 | + } from "sap/ui/model/json/TypedJSONModel"; |
| 235 | + |
| 236 | + /** |
| 237 | + * TypedJSONContext is a subclass of Context that provides type-safe access to the model data. It is only available when using UI5 with TypeScript. |
| 238 | + * |
| 239 | + * @since 1.140.0 |
| 240 | + */ |
| 241 | + export default class TypedJSONContext< |
| 242 | + Data extends object, |
| 243 | + Root extends AbsoluteBindingPath<Data>, |
| 244 | + > extends Context { |
| 245 | + constructor(oModel: TypedJSONModel<Data>, sPath: Root); |
| 246 | + |
| 247 | + getModel(): TypedJSONModel<Data>; |
| 248 | + |
| 249 | + getProperty<P extends RelativeBindingPath<Data, Root>>( |
| 250 | + sPath: P extends RelativeBindingPath<Data, Root> ? P : never, |
| 251 | + ): PropertyByRelativeBindingPath<Data, Root, P>; |
| 252 | + } |
| 253 | +} |
0 commit comments