Skip to content

Commit 48f1c25

Browse files
authored
🤖 Merge PR DefinitelyTyped#72427 Fix for not returning null for array get commands by @daryllabar
1 parent 7afd625 commit 48f1c25

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

‎types/xrm/index.d.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,18 @@ declare namespace Xrm {
11691169
*/
11701170
getAttribute(
11711171
delegateFunction?: Collection.MatchingDelegate<Attributes.Attribute>,
1172-
): Collection.ItemCollection<Attributes.Attribute> | null;
1172+
): Attributes.Attribute[];
1173+
1174+
/**
1175+
* Gets a collection of attributes using a delegate function or gets all attributes if delegateFunction is not provided.
1176+
* @param T An Attribute type.
1177+
* @param delegateFunction A matching delegate function
1178+
* @returns An collection of attributes.
1179+
* @see {@link https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/collections External Link: Collections (Client API reference)}
1180+
*/
1181+
getAttribute<T extends Attributes.Attribute>(
1182+
delegateFunction?: Collection.MatchingDelegate<Attributes.Attribute>,
1183+
): T[];
11731184

11741185
/**
11751186
* Gets a control by name or index.
@@ -1194,7 +1205,18 @@ declare namespace Xrm {
11941205
*/
11951206
getControl(
11961207
delegateFunction?: Collection.MatchingDelegate<Controls.Control>,
1197-
): Collection.ItemCollection<Controls.Control> | null;
1208+
): Controls.Control[];
1209+
1210+
/**
1211+
* Gets a collection of controls using a delegate function or gets all controls if delegateFunction is not provided.
1212+
* @param T A Control type. There is no guarentee that all control types will match.
1213+
* @param delegateFunction A matching delegate function.
1214+
* @returns An collection of controls.
1215+
* @see {@link https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/collections External Link: Collections (Client API reference)}
1216+
*/
1217+
getControl<T extends Controls.Control>(
1218+
delegateFunction?: Collection.MatchingDelegate<Controls.Control>,
1219+
): T[];
11981220
}
11991221

12001222
/**
@@ -1859,7 +1881,7 @@ declare namespace Xrm {
18591881
* @param delegate A matching delegate function
18601882
* @returns A T[] whose members have been validated by delegate or a entire array of T[]
18611883
*/
1862-
get(delegate?: MatchingDelegate<T>): T[] | null;
1884+
get(delegate?: MatchingDelegate<T>): T[];
18631885

18641886
/**
18651887
* Gets the length of the collection.

‎types/xrm/xrm-tests.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,38 @@ function ActionOnPostsave(context: Xrm.Events.PostSaveEventContext) {
723723
console.log(args.getSaveErrorInfo());
724724
}
725725
}
726+
727+
// Demonstrate non-index/name getAttribute/getControl returns non-null array
728+
729+
function GetAll(context: Xrm.FormContext) {
730+
const allAttributes: Xrm.Attributes.Attribute[] = context.getAttribute();
731+
const allAttributesTyped: Xrm.Attributes.StringAttribute[] = context.getAttribute<Xrm.Attributes.StringAttribute>();
732+
const noAttributes: Xrm.Attributes.Attribute[] = context.getAttribute(() => false);
733+
const noAttributesTyped: Xrm.Attributes.StringAttribute[] = context.getAttribute<Xrm.Attributes.StringAttribute>(
734+
() => false,
735+
);
736+
if (
737+
allAttributes === null
738+
|| allAttributesTyped === null
739+
|| noAttributes === null
740+
|| noAttributesTyped === null
741+
) {
742+
throw new Error("Will return an empty array if no attributes are present.");
743+
}
744+
745+
const allControls: Xrm.Controls.Control[] = context.getControl();
746+
const allControlsTyped: Xrm.Controls.StringControl[] = context.getControl<Xrm.Controls.StringControl>();
747+
const itemCollection: Xrm.Controls.Control[] = (context.getControl("mySection") as unknown as Xrm.Controls.Section)
748+
.controls.get();
749+
const noControls: Xrm.Controls.Control[] = context.getControl(() => false);
750+
const noControlsTyped: Xrm.Controls.StringControl[] = context.getControl<Xrm.Controls.StringControl>(() => false);
751+
if (
752+
allControls === null
753+
|| allControlsTyped === null
754+
|| itemCollection === null
755+
|| noControls === null
756+
|| noControlsTyped === null
757+
) {
758+
throw new Error("Will return an empty array if no controls are present.");
759+
}
760+
}

0 commit comments

Comments
 (0)