Skip to content

Commit 72de7cf

Browse files
gncnpkGavin Canon-Phratsachack
andauthored
🤖 Merge PR DefinitelyTyped#72432 [xrm] Add getAttributeType method to specific attribute interfaces, changes getControlType method to use template literal instead of string by @gncnpk
Co-authored-by: Gavin Canon-Phratsachack <[email protected]>
1 parent 7b9b59b commit 72de7cf

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

‎types/xrm/index.d.ts‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,6 +2753,11 @@ declare namespace Xrm {
27532753
* @see {@link Attribute}
27542754
*/
27552755
interface NumberAttribute extends Attribute<number> {
2756+
/**
2757+
* Get the attribute type.
2758+
* @returns The attribute type.
2759+
*/
2760+
getAttributeType(): "integer" | "decimal" | "double" | "money";
27562761
/**
27572762
* Gets the attribute format.
27582763
* @returns The format of the attribute.
@@ -2797,6 +2802,11 @@ declare namespace Xrm {
27972802
* @see {@link Attribute}
27982803
*/
27992804
interface StringAttribute extends Attribute<string> {
2805+
/**
2806+
* Get the attribute type.
2807+
* @returns The attribute type.
2808+
*/
2809+
getAttributeType(): "string";
28002810
/**
28012811
* Gets the attribute format.
28022812
* @returns The format of the attribute.
@@ -2846,17 +2856,17 @@ declare namespace Xrm {
28462856
* @see {@link EnumAttribute}
28472857
*/
28482858
interface BooleanAttribute extends EnumAttribute<boolean> {
2849-
/**
2850-
* A collection of all the controls on the form that interface with this attribute.
2851-
* @see {@link https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/collections External Link: Collections (Client API reference)}
2852-
*/
2853-
controls: Collection.ItemCollection<Controls.BooleanControl>;
2854-
28552859
/**
28562860
* Gets the attribute format.
28572861
* @returns the string "boolean"
28582862
*/
28592863
getAttributeType(): "boolean";
2864+
2865+
/**
2866+
* A collection of all the controls on the form that interface with this attribute.
2867+
* @see {@link https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/collections External Link: Collections (Client API reference)}
2868+
*/
2869+
controls: Collection.ItemCollection<Controls.BooleanControl>;
28602870
}
28612871

28622872
/**
@@ -2865,6 +2875,12 @@ declare namespace Xrm {
28652875
* @see {@link Attribute}
28662876
*/
28672877
interface DateAttribute extends Attribute<Date> {
2878+
/**
2879+
* Gets the attribute type.
2880+
* @returns The attribute type.
2881+
*/
2882+
getAttributeType(): "datetime";
2883+
28682884
/**
28692885
* Gets the attribute format.
28702886
*
@@ -2885,6 +2901,12 @@ declare namespace Xrm {
28852901
* @see {@link EnumAttribute}
28862902
*/
28872903
interface OptionSetAttribute<T extends number = number> extends EnumAttribute<T> {
2904+
/**
2905+
* Gets the attribute type.
2906+
* @returns The attribute type.
2907+
*/
2908+
getAttributeType(): "optionset";
2909+
28882910
/**
28892911
* Gets the attribute format.
28902912
* @returns The format of the attribute.
@@ -2946,6 +2968,11 @@ declare namespace Xrm {
29462968
* @see {@link EnumAttribute}
29472969
*/
29482970
interface MultiSelectOptionSetAttribute<T extends number = number> extends EnumAttribute<T[]> {
2971+
/**
2972+
* Gets the attribute type.
2973+
* @returns The attribute type.
2974+
*/
2975+
getAttributeType(): "multiselectoptionset";
29492976
/**
29502977
* Gets the attribute format.
29512978
* @returns The format of the attribute.
@@ -3008,6 +3035,11 @@ declare namespace Xrm {
30083035
* @see {@link Attribute}
30093036
*/
30103037
interface LookupAttribute extends Attribute<LookupValue[]> {
3038+
/**
3039+
* Gets the attribute type.
3040+
*/
3041+
getAttributeType(): "lookup";
3042+
30113043
/**
30123044
* Gets a boolean value indicating whether the Lookup is a multi-value PartyList.
30133045
* @returns true the attribute is a PartyList, otherwise false.
@@ -3275,7 +3307,7 @@ declare namespace Xrm {
32753307
* * customcontrol: <namespace>.<name> (A custom control for mobile phone and tablet clients).
32763308
* * customsubgrid: <namespace>.<name> (A custom dataset control for mobile phone and tablet clients).
32773309
*/
3278-
getControlType(): ControlType | string;
3310+
getControlType(): ControlType | `${string}.${string}`;
32793311

32803312
/**
32813313
* Gets the name of the control on the form.

‎types/xrm/xrm-tests.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ if (attribute !== null) {
224224
attribute.setSubmitMode(submitModeString); // Works if the string is a const
225225
attribute.setRequiredLevel(requirementLevel);
226226
attribute.setRequiredLevel(requirementLevelString); // Works if the string is a const
227-
228-
const isMulitselect = attribute.getAttributeType() === "multiselectoptionset";
227+
// @ts-expect-error
228+
const isMultiSelectAttribute = attribute.getAttributeType() === "multiselectoptionset"; // This will return false because the attribute is a LookupAttribute
229229
}
230230
/// Demonstrate v8.2 quick form controls
231231

@@ -758,3 +758,14 @@ function GetAll(context: Xrm.FormContext) {
758758
throw new Error("Will return an empty array if no controls are present.");
759759
}
760760
}
761+
762+
function testAttributeType(formContext: Xrm.FormContext) {
763+
const attribute = formContext.getAttribute<Xrm.Attributes.StringAttribute>("myattribute");
764+
if (attribute === null) {
765+
return;
766+
}
767+
const attributeType = attribute.getAttributeType();
768+
// @ts-expect-error
769+
const isNumberAttribute = attributeType === "number"; // This errors because the attribute is a StringAttribute, not a NumberAttribute
770+
const isStringAttribute = attributeType === "string"; // This works because the attribute is a StringAttribute
771+
}

0 commit comments

Comments
 (0)