Skip to content

Commit c488131

Browse files
authored
🤖 Merge PR DefinitelyTyped#74879 [@types/xrm] Fix ItemCollection.get() overload order to correctly return T | null by @Mosh-K
1 parent 1102a6a commit c488131

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎types/xrm/index.d.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,14 +1862,14 @@ declare namespace Xrm {
18621862
* @param itemNameOrNumber The item name or item number to get.
18631863
* @returns The T matching the key itemName or the T in the itemNumber-th place.
18641864
*/
1865-
get<TSubType extends T>(itemNameOrNumber: string | number): TSubType;
1865+
get(itemNameOrNumber: string | number): T | null;
18661866

18671867
/**
18681868
* Gets the item given by key or index.
18691869
* @param itemNameOrNumber The item name or item number to get.
18701870
* @returns The T matching the key itemName or the T in the itemNumber-th place.
18711871
*/
1872-
get(itemNameOrNumber: string | number): T | null;
1872+
get<TSubType extends T>(itemNameOrNumber: string | number): TSubType;
18731873

18741874
/**
18751875
* Gets the item using a delegate matching function or the entire array of T if delegate is not provided.

‎types/xrm/xrm-tests.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,13 @@ function booleanAttributeControls(formContext: Xrm.FormContext) {
630630
// @ts-expect-error
631631
const notString: string = booleanAttribute.getValue();
632632

633-
booleanAttribute = booleanAttribute.controls.get(0).getAttribute();
633+
booleanAttribute = booleanAttribute.controls.get<Xrm.Controls.BooleanControl>(0).getAttribute();
634634

635635
booleanAttribute.controls.forEach((c: Xrm.Controls.BooleanControl) => c.setDisabled(true));
636636

637-
booleanAttribute.controls.get(0).getAttribute().getAttributeType() === "boolean";
637+
booleanAttribute.controls.get<Xrm.Controls.BooleanControl>(0).getAttribute().getAttributeType() === "boolean";
638638
// @ts-expect-error
639-
booleanAttribute.controls.get(0).getAttribute().getAttributeType() === "optionset";
639+
booleanAttribute.controls.get<Xrm.Controls.OptionSetControl>(0).getAttribute().getAttributeType() === "optionset";
640640
}
641641

642642
// Demonstrate add and remove methods for formContext.data.process
@@ -799,3 +799,20 @@ const framedControlSetVisible = (formContext: Xrm.FormContext) => {
799799
// setVisible
800800
framedControl.setVisible(true);
801801
};
802+
803+
// Demonstrate ItemCollection.get() overloads
804+
function testItemCollectionGet(formContext: Xrm.FormContext) {
805+
// Without explicit type parameter: returns T | null
806+
// $ExpectType Tab | null
807+
formContext.ui.tabs.get(0);
808+
809+
// $ExpectType Tab | null
810+
formContext.ui.tabs.get("tabName");
811+
812+
// With explicit type parameter: returns TSubType (caller asserts item exists)
813+
// $ExpectType Tab
814+
formContext.ui.tabs.get<Xrm.Controls.Tab>(0);
815+
816+
// $ExpectType Tab
817+
formContext.ui.tabs.get<Xrm.Controls.Tab>("tabName");
818+
}

0 commit comments

Comments
 (0)